0

I'm trying to add text to the product short description by category.

So for example, for all Cake Toppers, I want to add to the bottom of the short description to show "This is a cake topper". For all products which are not cake toppers, I want to show the text "Not a cake topper".

This is the code I'm using:

add_action( 'woocommerce_single_product_summary', 'product_short_description_by_category', 20 );

function product_short_description_by_category() {
if( is_product_category('cake-toppers') ){
   echo '<p>A cake topper</p>';
    }else{ 
    echo '<p>NOT CAKE TOPPER</p>';
    }
}

But all products, including cake topper, show "THIS IS NOT A CAKE TOPPER".

What am I doing wrong here? "cake-toppers" is the slug for the category name.

Site at: http://wendyw11.sg-host.com/product/two-wild-cake-topper/

Any help is greatly appreciated!

1

1 Answer 1

0

is_product_category() – Check If Current Page is a Product Category

Try this

add_action( 'woocommerce_single_product_summary', 'product_short_description_by_category', 20 );

function product_short_description_by_category() { 

        if( has_term( 'cake-toppers', 'product_cat' )){ 

                echo 'A cake topper'; 
        } else{ 
                echo 'NOT CAKE TOPPER'; 
        } 
}
2
  • Hi Valerii, thank you, that does work! Would you mind explaining why my original code does not work?
    – Wendy
    Commented Sep 20, 2020 at 9:57
  • The reason is that is_product_category() tells you if you're browsing a product category, not if the current product has a particular category. Commented Sep 21, 2020 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.