First of all, Woocommerce products are stored on WordPress as custom post type (CPT) named ‘product’. The product categories are custom taxonomy named ‘product_cat’.
Let’s say you want to show a simple line of text that tells your visitors the shipping flat rate of a product, and you have different rates for different product categories. For an example, you have two different product categories in your shop, which you named them as tshirts and hoodies.
Tshirts have shipping flat rate of $10.00 while hoodies have flat rate of $15.00.
This is how you check for current product category (either tshirts or hoodies) on a single product page. This is used inside the loop.
<?php | |
/* | |
URL: https://wproot.dev/blog/get-woocommerce-current-product-category-single-product-page/ | |
*/ | |
if ( has_term( 'tshirts', 'product_cat' ) ) //product category is tshirts | |
{ | |
$flatrate = '10.00'; | |
} | |
elseif ( has_term( 'hoodies', 'product_cat' ) ) //product category is hoodies | |
{ | |
$flatrate = '15.00'; | |
} | |
else | |
{ | |
$flatrate = '0.00'; | |
} | |
echo 'USD'.$flatrate.' Flat Rate Shipping'; | |
?> |
That’s it!