product list php id:
For my WC product pages, I need to add a class to the body tag so that I can perform some custom styling. Hereâs the function Iâm creating for thisâŠ
function my_add_woo_cat_class($classes) {
$wooCatIdForThisProduct = â?????â; //help!
// add âclass-nameâ to the $classes array
$classes[] = âmy-woo-cat-id-â . $wooCatIdForThisProduct;
// return the $classes array
return $classes;
}
//If weâre showing a WC product page
if (is_product()) {
// Add specific CSS class by filter
add_filter(âbody_classâ,âmy_add_woo_cat_classâ);
}
âŠbut how do I get the WooCommerce cat ID?
To get the WooCommerce category ID for a product, you can use the wc_get_product_category_ids()
function provided by WooCommerce. This function will return an array of category IDs associated with the product, and you can use this information to add the necessary class to the body tag.
Hereâs how you can modify your existing function to get the WooCommerce category ID for the current product:
php
<span class="hljs-function"> <span class="hljs-keyword">function</span> <span class="hljs-title">my_add_woo_cat_class</span>( <span class="hljs-params"> <span class="hljs-variable">$classes</span> </span>) </span>{ <span class="hljs-comment">// Check if we're on a WooCommerce product page</span> <span class="hljs-keyword">if</span> ( <span class="hljs-title function_ invoke__">is_product</span>()) { <span class="hljs-comment">// Get the current product</span> <span class="hljs-keyword">global</span> <span class="hljs-variable">$product</span>; <span class="hljs-comment">// Check if the product is valid</span> <span class="hljs-keyword">if</span> ( <span class="hljs-variable">$product</span> && <span class="hljs-title function_ invoke__">is_a</span>( <span class="hljs-variable">$product</span>, <span class="hljs-string">'WC_Product'</span>)) { <span class="hljs-comment">// Get the category IDs associated with the product</span> <span class="hljs-variable">$category_ids</span> = <span class="hljs-title function_ invoke__">wc_get_product_category_ids</span>( <span class="hljs-variable">$product</span>-> <span class="hljs-title function_ invoke__">get_id</span>()); <span class="hljs-comment">// If there are category IDs associated with the product</span> <span class="hljs-keyword">if</span> (! <span class="hljs-keyword">empty</span>( <span class="hljs-variable">$category_ids</span>)) { <span class="hljs-comment">// Use the first category ID (you can modify this as per your requirements)</span> <span class="hljs-variable">$wooCatIdForThisProduct</span> = <span class="hljs-variable">$category_ids</span>[ <span class="hljs-number">0</span>]; <span class="hljs-comment">// Add the class to the $classes array</span> <span class="hljs-variable">$classes</span>[] = <span class="hljs-string">'my-woo-cat-id-'</span> . <span class="hljs-variable">$wooCatIdForThisProduct</span>; } } } <span class="hljs-comment">// Return the modified $classes array</span> <span class="hljs-keyword">return</span> <span class="hljs-variable">$classes</span>; } <span class="hljs-comment">// If we're showing a WC product page, add specific CSS class by filter</span> <span class="hljs-keyword">if</span> ( <span class="hljs-title function_ invoke__">is_product</span>()) { <span class="hljs-title function_ invoke__">add_filter</span>( <span class="hljs-string">'body_class'</span>, <span class="hljs-string">'my_add_woo_cat_class'</span>); }
In this code, we first check if we are on a WooCommerce product page using is_product()
. If we are, we retrieve the current product using global $product
and then check if it is a valid instance of WC_Product
. If it is, we get the category IDs associated with the product using wc_get_product_category_ids()
. If there are category IDs available, we use the first one (you can modify this based on your specific needs) and append the class to the $classes
array.
With this code, the body tag on your WooCommerce product pages will have the custom class based on the productâs category ID, allowing you to perform custom styling as needed.