WooCommerce PHP Beginner Requires WooCommerce

Add a “Sold Out” Badge on the Shop Page

Show a clear "Sold out" badge on out-of-stock products in the shop grid, so shoppers don't click through to find out.

Tested on WordPress 7.1 PHP 8.3 Updated
functions.php 10 lines
add_action( 'woocommerce_before_shop_loop_item_title', function (): void {
	global $product;
	if ( $product instanceof WC_Product && ! $product->is_in_stock() ) {
		echo '<span class="nahnu-sold-out">Sold out</span>';
	}
}, 9 );

add_action( 'wp_head', function (): void {
	echo '<style>.woocommerce ul.products li.product{position:relative}.nahnu-sold-out{position:absolute;top:10px;left:10px;z-index:2;padding:4px 10px;border-radius:999px;background:#15131F;color:#fff;font-size:12px;font-weight:700;text-transform:uppercase}</style>';
} );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

If you keep sold-out products visible, shoppers should see their status at a glance. This snippet adds a “Sold out” badge to their image in the shop grid.

How it works

The badge is printed on woocommerce_before_shop_loop_item_title at priority 9, just before the product image. It only appears when is_in_stock() is false.

A few lines of CSS position it in the top-left corner of the product card. Adjust the colours to match your brand.

Things to watch

  • If you’d rather hide sold-out products, tick “Hide out of stock items from the catalog” under WooCommerce → Settings → Products → Inventory instead.
  • Products on backorder count as in stock and won’t get the badge.

How to test it

Set a product to Out of stock and open the shop page. Its image should show a “Sold out” badge.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only