WooCommerce: “Sale” Category (Automatic)

You can use a shortcode or block in order to display the WooCommerce products on sale. However, what if you wanted a proper “product category” called “Sale” – and where you didn’t need to manually assign this category to each product?

Basically, how do we display all the discounted products in a custom category called “Sale”, without doing any manual work?

Here’s a super quick tutorial. Enjoy!

My “Sale” product category contains 16 products. With the snippet below I didn’t have to go into each product and tick this category by hand… it’s just all automatic!

Step 1: Add New Product Category

A one-off manual step is required, actually.

Simply go to WP Dashboard > Products > Categories > Add new category and enter the category name e.g. “Sale” and its slug e.g. “sale”.

The slug is very important as it’s used in the snippet below, so if you decide to rename it, you must change the PHP accordingly.

Step 2: PHP Snippet To Programmatically Populate the “Sale” Product Category With Products On Sale

/**
 * @snippet       Automatically Populate Sale Product Cat
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 6
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_action( 'woocommerce_product_query', 'bbloomer_sale_category' );

function bbloomer_sale_category( $q ) {
   if ( "sale" !== $q->get( 'product_cat' ) ) return; // "sale" = slug
	$q->set( 'post_type', 'product' );
	$q->set( 'product_cat', null );
	$product_ids_on_sale = wc_get_product_ids_on_sale() ? wc_get_product_ids_on_sale() : array();
	$q->set( 'post__in', $product_ids_on_sale );
}

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (delete "?>" if you have it there). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.3.

If you think this code saved you time & money, feel free to join 14,000+ WooCommerce Weekly subscribers for blog post updates or 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

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.

6 thoughts on “WooCommerce: “Sale” Category (Automatic)

  1. Dont work for variable products ๐Ÿ™

    1. Hello Ivo, please expand on that? The wc_get_product_ids_on_sale function should indeed return variable products IDs as well. Is your DB table “wc_product_meta_lookup” up to date?

  2. And will it work with variable products too?
    And will it remove the products automatically when they are not on sale anymore?
    And I do not want products from a category to be included, how do I implement this in your code?

    1. Thanks for your feedback!

      1) it should
      2) it will
      3) I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

      1. Thanks for your reply.
        Your code works but the products do not appear in the category SALE. When I go to Woocommerce -> products -> filter on category: Sale… that product is not there…

        1. Backend or frontend? Because in the backend it won’t show any products, this simply populates the frontend category “Sale”

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. 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 BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

Your email address will not be published.