2

I have added badges to my product images to display important information. If a product has a specific tag, it will display that specific badge. I got this to work in the shop page and single product page, but having trouble with the cart page. I can load one badge to all the items in the cart, but can't seem to load the other badges conditionally.

These are the conditional statements I used for the shop page and single product page:

global $product;
if ( $product->is_on_sale() ) {
echo '<span class="product-badge">SALE</span>';
}elseif ( has_term( 'reversible', 'product_tag' ) ) {      
echo '<span class="product-badge">REVERSIBLE</span>';
} elseif ( has_term( 'hard-sole', 'product_tag' ) ) {
echo '<span class="product-badge">HARD SOLE</span>';
} elseif ( has_term( 'soft-sole', 'product_tag' ) ) {
echo '<span class="product-badge">SOFT SOLE</span>';
}

This is the code I have so far for the cart page:

add_filter( 'woocommerce_cart_item_thumbnail', 'cart_badge' );

function cart_badge( $thumbnail ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
if ( has_term( 'reversible', 'product_tag', $cart_item['product_id'] ) ) 
{
printf( '<a href="%s">%s<span class="cart-badge">REVERSIBLE</span></a>', 
esc_url( $product_permalink ), $thumbnail ); // PHPCS: XSS ok.
}
}
}

How do I add apply my conditional statements to my current code to show the specific product badge on specific product thumbnail, or else echo $thumbnail if conditions are false?

  • this url help you for archive your goal - https://stackoverflow.com/questions/41104409/get-in-woocommerce-cart-the-product-id-of-a-cart-item – Zaheer Ahmad Sep 23 '19 at 07:29

0 Answers0