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        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 6
 * @community     https://businessbloomer.com/club/
 */

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

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

  1. Hi! Our theme seems to be using your code, but for some reason it is giving the page a diffrent and wrong boxed layout. Do you have any idea why?

    1. I don’t see any differences. Screenshot please? My code only works on the query, so does nothing in regard to the layout

  2. Hi, i saw this “You can use a shortcode or block in order to display the WooCommerce products on sale.” but after searching, i cannot find a way where woocommerce natively displays all products on sale, can you help? Thanks!

    1. If you use Woo blocks, add the “products” block and filter the query by products on sale only. Otherwise, try with this shortcode https://woocommerce.com/document/woocommerce-shortcodes/products/#scenario-1-random-sale-items

  3. Hey, thanks for this code, it’s been very helpful. However, the sale page ends up display products with sales that are scheduled even when the sale isn’t currently active, eg I will set up a sale with an end date and after that date there will no longer be a price decrease but the product will still appear in the sale category page. Is there a way to fix this?

    1. Hello Marly, I just tried and it works fine on my end. I had 26 products on sale; for one of them, I edited the sale start date to be from tomorrow, and the products became 25. so there must be something else messing things up within your setup

  4. Hello! Thanks for your work. I really admire people like you, who create amazing things to make people’s lives easier.

    I’m writing to you because I’ve tried to use the code you worked on to automatically list all the products that have a discount on a single page. I inserted your code in the functions panel of my template, and it really works, it lists all the products that have a discount. But I’m getting an error in the filters sidebar. Does this error have to do with the fact that the code I’m trying to use doesn’t work for variable products? Can you help me please? I want to understand what could be happening.

    Thank you very much again for your contribution. A hug from Colombia 🙂

    1. What error are you getting exactly? Can you share a screenshot please? Saludos!

      1. Hi Rodolfo, I didn’t know you had answered me. Look, I have a filter that filters the “color”, with 3 options, “blue”, “white” and “cream”. But in each of the 2 options, these error messages appear:

        (this is the tittle of the filter->) COLOR

        Warning: strstr() expects parameter 1 to be string, object given in /home/finalgood/public_html/wp-includes/functions.php on line 1147

        Warning: stripos() expects parameter 1 to be string, object given in /home/finalgood/public_html/wp-includes/functions.php on line 1154

        Warning: stripos() expects parameter 1 to be string, object given in /home/finalgood/public_html/wp-includes/functions.php on line 1157

        Warning: strpos() expects parameter 1 to be string, object given in /home/finalgood/public_html/wp-includes/compat.php on line 456

        Warning: explode() expects parameter 2 to be string, object given in /home/finalgood/public_html/wp-includes/functions.php on line 1165
        Blue

        Warning: strstr() expects parameter 1 to be string, object given in /home/finalgood/public_html/wp-includes/functions.php on line 1147

        Warning: stripos() expects parameter 1 to be string, object given in /home/finalgood/public_html/wp-includes/functions.php on line 1154

        Warning: stripos() expects parameter 1 to be string, object given in /home/finalgood/public_html/wp-includes/functions.php on line 1157

        Warning: strpos() expects parameter 1 to be string, object given in /home/finalgood/public_html/wp-includes/compat.php on line 456

        Warning: explode() expects parameter 2 to be string, object given in /home/finalgood/public_html/wp-includes/functions.php on line 1165
        White

        Warning: strstr() expects parameter 1 to be string, object given in /home/finalgood/public_html/wp-includes/functions.php on line 1147

        Warning: stripos() expects parameter 1 to be string, object given in /home/finalgood/public_html/wp-includes/functions.php on line 1154

        Warning: stripos() expects parameter 1 to be string, object given in /home/finalgood/public_html/wp-includes/functions.php on line 1157

        Warning: strpos() expects parameter 1 to be string, object given in /home/finalgood/public_html/wp-includes/compat.php on line 456

        Warning: explode() expects parameter 2 to be string, object given in /home/finalgood/public_html/wp-includes/functions.php on line 1165
        Cream

        1. No prob! As per your error messages, my snippet does not use strpos() stripos() explode() and strstr() functions, so it must be a problem with your filter plugin or another plugin causing a conflict

  5. Hello, works very well, but it doesnt work with the filter. Filter works on every page but not on the sale page I get the following error:

    arning: strstr() expects parameter 1 to be string, object given in /home/customer/www/ mydomain.com/public_html/wp-includes/functions.php on line 1147

    Warning: stripos() expects parameter 1 to be string, object given in /home/customer/www/ mydomain.com/public_html/wp-includes/functions.php on line 1154

    Warning: strpos() expects parameter 1 to be string, object given in /home/customer/www/ mydomain.com/public_html/wp-includes/compat.php on line 473

    Warning: explode() expects parameter 2 to be string, object given in /home/customer/www/ mydomain.com/public_html/wp-includes/functions.php on line 1165

    When I remove the code it works fine. How can I fix this?

    1. This strictly depends on the filter you’re using, so the function may need some tweaking.

      1. THank you, it’s the standard filter of Woocommerce to filter products on size , color etc, all category pages this works only on sale it gives an error. Do you know why?

        1. Products are not “assigned” to the category, this code only shows products in the category view. Something different may be needed to also make it work with filters

  6. The snippet doesn’t work with php 8.2

    1. Why? What error do you get?

  7. Hello,

    Thanks for your code, it works for me.
    But it does not put the products in the category: sale.
    There is no checkmark in the sales category checkbox on the product page, and if I go to the category sale itself in the back end. The product isn’t in there either.

    Can you make it, so the products are placed in and out of the sale category?

    Greetings

    1. Just curious – why do you need them there physically?

  8. Yes, as Kevin suggested, this line of code

    $q->set( 'product_cat', null );

    gives the mentioned error. I also tried your fix to change to:

    $product_ids_on_sale = wc_get_product_ids_on_sale();

    But it does now tork.

  9. This code snippet does not work with PHP 8.2. It does work on PHP 7.4. Do you have a revision that works with newer versions of PHP?

    1. Hello Brooke, the snippet doesn’t really feature any PHP function apart from the ternary operator, which is properly written. Can you test again please, and if you’re still having problems maybe share the error log so I can have more context about the error please?

      1. I have the same problem as Brooke. On my stage env with 7.4 PHP the snippet works fine. When I change the PHP ver. to 8.x my site gonna be broken.

        1. And what error do you get?

          1. Hmm.. only what I got is this log:

            CRITICAL Uncaught TypeError: strstr(): Argument #1 ($haystack) must be of type string, WP_Error given in /----/public_html/wp-includes/functions.php:1144
            1. Thank you. That doesn’t seem related to my snippet though. If you remove my snippet does this go away?

              1. yes! When I switched off the snippet it works fine.

                1. PHP version? I’m on 7.4.33

                  1. 8.2 🙂

                    1. And if you temporarily change this line:

                      $product_ids_on_sale = wc_get_product_ids_on_sale() ? wc_get_product_ids_on_sale() : array();

                      to:

                      $product_ids_on_sale = wc_get_product_ids_on_sale();

                      or, alternatively, temporarily downgrade to PHP 7.4. does it work?

  10. Can this snippet be edited to apply to different “sale” categories based on the discount percentage the product has?
    Say for example:
    If the product is 10% off – category 1
    If the product is 20% off – category 2
    If the product is 30% off – category 3

    1. Hello Evan, thanks so much for your comment! Yes, this is definitely possible – if you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  11. Hi, how can I show only 1 category on the product archive?
    When I put everyone in “sale”, for example it appears like “women, sale”

    I want to hide Sale or only show the first category,

    Thankss!!!!

  12. Hi Rodolfo,

    Thanks for the snippet. This works nice!

    Just for your info: It is not compatible with Permalink Manager Pro unfortunately. When Permalink Manager Pro is active, the Sale category page shows nothing.

  13. I would love this functionality! However, I can’t get the snippet to work in Kadence Theme and WordPress 6.0.2.

    1. Well, apparently it does populate the Sale Category. I did not see it working immediately because the sale category itself did not appear on Shop Page together with all the available categories. Is there a way for the Sale Category to appear with the rest of the Categories at the shop page?

      1. Great! The Sale Category is a standard WooCommerce product category, so it should show

  14. Thanks for all your hard work, your site has helped me plenty with a starting point. But for this I seen that it works only on the frontend and I needed it to also show up on the backend too. After more research and testing I did finally got a code that works for adding it to the backend and frontend. Hopes this helps someone else.

    /**
     * @snippet       Automatically Populate Sale Product Cat
     * @source       https://thehorsebc.world/
     */
    
    add_action( 'woocommerce_update_product', 'update_product_set_sale_cat', 10, 2 );
    
    function update_product_set_sale_cat( $product_id, $product ) {
        if ( $product->is_on_sale() ) {
            wp_add_object_terms($product_id, "sale", 'product_cat');    // "sale" = slug
        } else { // remove the sale category when the product in no longer on sale
            wp_remove_object_terms($product_id, "sale", 'product_cat');   // "sale" = slug
        }
    }
    
      1. syntax error, unexpected ‘&’
        here: if ( $product->is_on_sale() ) {

        how can i fix that?

  15. 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?

  16. 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”

          1. Hi 🙂

            It is worth adding a condition if there are no products on sale.

               if(empty($q->get( 'post__in' ))) {
                $q->set( 'post__in', array(-1) );
               }
            

            Thx 😉

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 *