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!
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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';
}
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0