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

Subscribe to my newsletter and never miss my upcoming articles

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>';
}

✅ 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! 😎

No Comments Yet