Get Woocommerce Current Product Category In Single Product Page

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.

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';
$flatrate = '0.00';
echo 'USD'.$flatrate.' Flat Rate Shipping';

That’s it!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.