Someone asked me in my facebook page that how could he add “Sold Out” badge in WooCommerce product. Well, here’s how:
[php]
add_action( ‘woocommerce_before_shop_loop_item_title’, function() {
global $product;
if ( !$product->is_in_stock() ) {
echo ‘<span class="onsale soldout">Sold Out</span>’;
}
});
[/php]
Paste this snippet in your themes functions.php
and voila! Change the color as you wish using CSS, don’t ask me how 😉
Note: This code snippet requires your PHP version 5.3 or above.
O accha, tik ase 😉
Not working for me. could it be my theme is overriding this?
But for example…if I want to had a badge like “spicy” or “vegetarian” for some products…what can I do ?
Hi Raoul , here is the answer for your question : The Woocommerce Products Badge Management
http://codecanyon.net/item/woocommerce-products-badge-management/6770699?ref=Gema75
Ah, the plugin is new 😉
Hi Raoul – i get the following error when i add the code: Parse error: syntax error, unexpected T_FUNCTION in /…/…./…
separate the function and use its callback in the add_action.
Code should be this:
add_action( ‘woocommerce_before_shop_loop_item_title’, ‘sold_out_badge’ );
function sold_out_badge() {
global $product;
if ( !$product->is_in_stock() ) {
echo ‘Sold!’;
}
}
Thank you very much, made my day!!
Woocommerce Products Badge Management is not so useful and not really meet user’s need from my experience.
Is there a way to add a badge for each product. Like adding a snippet in function.php, then there is additional badge field in the product detail backend to fill and that badge can showed up in the frontend! The position and the design of the badge should match up with current “Sale” badge.
Anyone know how to make this happen?
EMAIL: [email protected]
Robert
Thanks for this post and @chomachomachoma for their contribution. Very helpful 🙂