2

I need to display a 'new' badge to product on the archive page but only for products in the 'New' category

add_action( 'woocommerce_after_shop_loop_item_title', 'lottie_new_badge', 40 );

function lottie_new_badge($badge) {
global $product;

if (has_term ('new')) {
    echo '<p>New</p>';
}
return $badge;
}

Got this code but not working, mixed few bits of code together to try get it to work but no luck.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
FoamyMedia
  • 486
  • 3
  • 14
  • 32

1 Answers1

1

There is missing arguments when using has_term() function with Woocommerce product category. Also as you are using an action hook, $badge is not needed and not has to be returned like in a filter hook. So try the following instead:

add_action( 'woocommerce_after_shop_loop_item_title', 'display_lottie_new_badge', 40 );
function display_lottie_new_badge() {
    global $product;

    if ( has_term ( array('new'), 'product_cat', $product->get_id() ) ) {
        echo '<p class="new">New</p>';
    }
}

Code goes in function.php file of your active child theme (or active theme). It should works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399