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
 * @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

  • WooCommerce Visual Hook Guide: Single Product Page
    Here’s a visual hook guide for the WooCommerce Single Product Page. This is part of my “Visual Hook Guide Series“, through which you can find WooCommerce hooks quickly and easily by seeing their actual locations (and you can copy/paste). If you like this guide and it’s helpful to you, let me know in the comments! […]
  • WooCommerce: Disable Variable Product Price Range $$$-$$$
    You may want to disable the WooCommerce variable product price range which usually looks like $100-$999 when variations have different prices (min $100 and max $999 in this case). With this snippet you will be able to hide the highest price, and add a “From: ” prefix in front of the minimum price. At the […]
  • WooCommerce: Hide Price & Add to Cart for Logged Out Users
    You may want to force users to login in order to see prices and add products to cart. That means you must hide add to cart buttons and prices on the Shop and Single Product pages when a user is logged out. All you need is pasting the following code in your functions.php (please note: […]
  • WooCommerce Visual Hook Guide: Archive / Shop / Cat Pages
    I’ve created a visual HTML hook guide for the WooCommerce Archive Page (which is the same page for the Shop, Category, Tag pages). This visual guide belongs to my “Visual Hook Guide Series“, that I’ve put together so that you can find WooCommerce hooks quickly and easily by seeing their actual locations (and you can […]
  • WooCommerce: Hide Prices on the Shop & Category Pages
    Interesting WooCommerce customization here. A client of mine asked me to hide/remove prices from the shop page and category pages as she wanted to drive more customers to the single product pages (i.e. increasing the click-through rate). As usual, a simple PHP snippet does the trick. I never recommend to use CSS to “hide” prices, […]

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 *