I'm trying to display a badge with "Exclusive" text to a specific product in the store page or category archive or whenever this specific product cycle item is shown.
But I tried adding _action before _shop_loop_item, but the problem is that the $product
variable does not contain the object. I'm thinking of $product->get_id()
and if it matches the product id, apply some HTML to that specific product loop item.
add_action('woocommerce_before_shop_loop_item', 'add_custom_badge', 1); function add_custom_badge( $product ) { if ( $product->get_id() === 123 ) { echo '<script>console.log("add_custom_badge")</script>'; } }
By the way, get_id()
cannot be executed because $product
appears to be empty. This is where I stack up.
Yes, the location where I want the HTML to be printed is woocommerce_before_shop_loop_item
- just before the sales badge.
Any suggestions on how to filter loop items?
By default,
$product
is not passed to the callback function at thewoocommerce_before_shop_loop_item
hook. That's why it doesn't workUse instead
global $product
So you get: