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 ‘SALE’;
}elseif ( has_term( ‘reversible’, ‘product_tag’ ) ) {
echo ‘REVERSIBLE’;
} elseif ( has_term( ‘hard-sole’, ‘product_tag’ ) ) {
echo ‘HARD SOLE’;
} elseif ( has_term( ‘soft-sole’, ‘product_tag’ ) ) {
echo ‘SOFT SOLE’;
}
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( ‘%sREVERSIBLE‘,
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? I have tried add_action as well and can get multiple badges showing with if statement, but all the badges show on all the products.
Hi Alexbahena
Thanks for reaching out.
We’re a small operation with limited resources. To remain viable, our free support is limited to basic usage and troubleshooting. We’re able to offer limited support for custom development within our premium support scope where we have the resources to do so. Sorry, I don’t have better news. Codeable.io is also an option if you’d ever like to hire a developer to assist.
Thank you for responding. I completely understand. If I were to purchase premium, would this request be too much to ask for?
Thanks for your understanding, it’s most appreciated.
Your question would technically be beyond our premium support scope. We could assist with this question but not a series of questions of a similar complex nature. Hope that helps a little :)