Home » WooCommerce: Show “Sold Out” @ Shop Page

WooCommerce: Show “Sold Out” @ Shop Page

by Tutor Aspire

Here’s another simple snippet that can easily help user experience and make sure a “sold out” badge shows on each out of stock product in the category & shop pages.

Not all themes allow this so you can use the snippet below to make it happen!

Show WooCommerce Sold Out Badge
Show WooCommerce Sold Out Badge

PHP Snippet 1: Show “Sold Out” on Shop and Archive Pages If You Have no Existing “Out Of Stock” Badge – WooCommerce

/**
 * @snippet       Display "Sold Out" on Loop Pages - WooCommerce
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @testedwith    WooCommerce 4.6
 * @donate $9     https://www.tutoraspire.com
 */

add_action( 'woocommerce_before_shop_loop_item_title', 'tutoraspire_display_sold_out_loop_woocommerce' );

function tutoraspire_display_sold_out_loop_woocommerce() {
    global $product;
    if ( ! $product->is_in_stock() ) {
        echo 'Sold Out';
    }
} 

And a bit of CSS:

/* CSS */

.soldout {
padding: 3px 8px;
text-align: center;
background: #222;
color: white;
font-weight: bold;
position: absolute;
top: 6px;
right: 6px;
font-size: 12px;
}

PHP Snippet 2: Change “Out Of Stock” to “Sold Out” on Shop and Archive Pages If You Have an Existing “Out Of Stock” Badge – WooCommerce

There’s a chance your theme or a custom plugin are already adding an “Out of Stock” badge on every out of stock products in the loop / category / shop pages.

In this case, your only choice is to “translate” the string:

/**
 * @snippet       "Out of Stock" to "Sold Out" on Loop Pages - WooCommerce
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @testedwith    WooCommerce 4.6
 * @donate $9     https://www.tutoraspire.com
 */

add_filter( 'gettext', 'tutoraspire_translate_out_stock_string', 9999, 3 );
  
function tutoraspire_translate_out_stock_string( $translated, $untranslated, $domain ) {
if ( ! is_admin() ) {
switch ( $translated ) {
case 'Out of Stock':
$translated = 'Sold Out';
break;
}
}   
return $translated;
}

I don’t code – is there a reliable plugin for that?

As many readers would love to code but don’t feel 100% confident with it, I decided to look for a reliable plugin that achieves the same (or even better) result.

In this case, I recommend the YITH WooCommerce Badge Management plugin. On top of displaying custom text badges (free version), you can also create CSS, image and advanced badges, assign product badges to specific products and/or categories, pick the badge position and much more.

But in case you wish to code, keep reading 🙂

You may also like