1

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..

3
  • Hello,please you can check that product category is exists or not and also count the number of category and base on that you have to add HTML text. Commented Aug 2, 2018 at 10:17
  • Can you give me an example code? I was not clear about the categories. The HTML has to be added when the product is in one of these 2 categories, so not in both.
    – Kevinn02
    Commented Aug 2, 2018 at 14:01
  • sorry i'm too late i'm happy to see that someone answered you so problems are resolved Commented Aug 3, 2018 at 12:14

2 Answers 2

3

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 ); 
6
  • Thanks for your code. Looks like you are pointing me to the right direction. When I add this code to functions.php, the website goes blank. Any ideas?
    – Kevinn02
    Commented Aug 2, 2018 at 14:59
  • Sorry, there was a missing closing ) on the if statement. I've updated the answer. Don't forget to change term-name to be the one that you're testing for!
    – Peter HvD
    Commented Aug 2, 2018 at 16:23
  • One more question: I want to add a link in the custom HTML (where Test is in your code). When I add a link, page goes blank again. Do you know why?
    – Kevinn02
    Commented Aug 3, 2018 at 6:49
  • You would need to share your code to be able to help with that!
    – Peter HvD
    Commented Aug 3, 2018 at 13:54
  • I added the code below
    – Kevinn02
    Commented Aug 3, 2018 at 14:54
0
function filter_woocommerce_short_description( $post_excerpt ) {
global $post;
if ( has_term( "axtschlag", "product_cat", $post->ID ) || has_term( "grate-goods", "product_cat", $post->ID ) ) {
    $post_excerpt .= "<br/>" . "<strong>Pick 'n Mix actie!</strong><br> Kies & mix de merken <a href="/axtschlag">Axtschlag</a> en Grate Goods en ontvang korting! Kies je 3 tot 5 producten dan ontvang je 5% korting, kies je 6 tot 10 producten dan ontvang je maar liefst 10% korting direct in je winkelwagen. Geldt ook i.c.m. met lopende acties.";
}
return $post_excerpt; 
};
add_filter( 'woocommerce_short_description','filter_woocommerce_short_description',10, 1 );
1
  • It's because you are enclosing your string in " but it has also got " quotes in it in the <a href="/axtschlag"> part. Replace the " within the string with ' instead. Also (not an error, just cleaner) you don't need to break the string, so the "<br/>" . "<strong> can be just "<br/><strong>. Finally, has_term() accepts an array of terms, so you can simplify that whole line down to if ( has_term( array("axtschlag","grate-goods"), "product_cat", $post->ID ) ) {.
    – Peter HvD
    Commented Aug 3, 2018 at 14:57

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.