WooCommerce: Hide Sale! Badge If Product Is Out Of Stock

If a product cannot be purchased because it is out of stock, why would you want to tell people that it’s on sale – only because it has a sale price?

That would probably clutter your shop and maybe get the customer to click on the wrong item just because it was standing out, only to find out they can’t purchase it!

So, let’s fix this little “design bug” in WooCommerce – let’s make sure the Sale! badge gets hidden in case the product is out of stock. Enjoy!

If this product is out of stock, I’d like the Sale! badge to go away, both on the single product and shop pages.

PHP Snippet: Remove “Sale!” If Current Product Is Out Of Stock @ WooCommerce Single Product & Shop Pages

Let me break down the code below:

  1. add_filter() function is used to edit specific data. In this case, we’re filtering the woocommerce_sale_flash output.
  2. The callback function bbloomer_no_sale_badge_if_out_of_stock is hooked to the woocommerce_sale_flash hook. This function will be called when the woocommerce_sale_flash hook is triggered.
  3. The function bbloomer_no_sale_badge_if_out_of_stock takes three parameters: $html, $post, and $product.
  4. Inside the function, it first checks if the product is not in stock using $product->is_in_stock() function. If the product is not in stock, it returns early, hence it does not output any HTML.
  5. If the product is in stock, it returns the original $html. This essentially means that if the product is in stock, the sale flash HTML remains unchanged.

In summary, this PHP code ensures that the sale flash badge is only displayed for products that are in stock. If a product is out of stock, the sale flash badge won’t be displayed.

/**
 * @snippet       No Sale Badge If Out Of Stock @ WooCommerce Product, Shop
 * @tutorial      Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 8
 * @community     Join https://businessbloomer.com/club/
 */

add_filter( 'woocommerce_sale_flash', 'bbloomer_no_sale_badge_if_out_of_stock', 9999, 3 );

function bbloomer_no_sale_badge_if_out_of_stock( $html, $post, $product ) {
   if ( ! $product->is_in_stock() ) return;
   return $html;
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

2 thoughts on “WooCommerce: Hide Sale! Badge If Product Is Out Of Stock

  1. Great explanation, very clear and easy to understand !

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *