DEV Community

loading...
Cover image for How to add a Sold Out snippet or badge on a particular product using Woocommerce?

How to add a Sold Out snippet or badge on a particular product using Woocommerce?

juanc4milo
Articles about technology, especially software development and personal stuff. Blogging at juanc4milo.dev
Originally published at juanc4milo.dev Updated on ・2 min read

This article is based on the description of how it would help solve a problem that I have encountered before related to the task ✍️ that @Chris Bongers left in Hashnode Bootcamp-II ⛺.

If you are setting up a Wordpress site with Woocommerce, notice when any product inventory is out of stock, there is nothing on the item catalog page to help you determine the product's stock availability.

For the customer it is annoying to click every product to see the stock availability.

To deal with that, there are 2 options that can help you :

✅ Function in PHP

In the * functions.php * file normally found in the Theme folder, insert the following code at the end of the file:

add_action ('woocommerce_after_shop_loop_item_title', 'talbiconsept_wc_template_loop_stock', 10);

function talbiconsept_wc_template_loop_stock () {
    product $ global;
    if ($ product-> stock management () && (int) $ product-> get_stock_quantity () <1)
    echo '<p class = "stock out of stock">' .__ ('Sold out!'). '</p>';
}
Enter fullscreen mode Exit fullscreen mode

✅ Add a plugin

In Wordpress Admin Desktop, go to the Plugins tab and look for "Out of Stock Badge for WooCommerce":

image.png

Then go to plugin settings and set text label, font size, font color, and background color:

image.png

And that is! 😎

Other articles that you might like...

Do you like it? You can buy me a beer if you want.

Originally published at juanc4milo.dev

Discussion (0)

Forem Open with the Forem app