WooCommerce HTML after short description if product is in specific category

I want to show HTML text after the short description on the product page, but only on specific products that are in a specific category (categories).

I think I have to use in_category, but I can’t figure out how to display the text right after the short description.

My preference is to work with a function/filter/action.

This code works:

function filter_woocommerce_short_description( $post_excerpt )   {
$your_msg='Test';
return $post_excerpt.'<br>'.$your_msg; 
};
add_filter( 'woocommerce_short_description','filter_woocommerce_short_description',10, 1 ); 

But this one works on all product pages..

2 Answers
2

Woocommerce’s product categories are custom taxonomy terms, so you need to use the taxonomy functions (eg, has_term()) rather than WordPress’ category ones.

function filter_woocommerce_short_description( $post_excerpt ) {
    global $post;
    if ( has_term( "term-name", "product_cat", $post->ID ) ) {
        $post_excerpt .= "<br/>" . "Test";
    }
    return $post_excerpt; 
};
add_filter( 'woocommerce_short_description','filter_woocommerce_short_description',10, 1 ); 

Leave a Comment