WooCommerce sold out badge on products listing

Someone asked me in my facebook page that how could he add “Sold Out” badge in WooCommerce product. Well, here’s how:

WooCommerce Sold Out badge

add_action( 'woocommerce_before_shop_loop_item_title', function() {
    global $product;

    if ( !$product->is_in_stock() ) {
        echo '<span class="onsale soldout">Sold Out</span>';
    }
});

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.

9 thoughts on “WooCommerce sold out badge on products listing

    • 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!’;
      }
      }

  1. 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: tradecross@outlook.com

    Robert

Leave a Reply