WooCommerce: Filter Products By Sale Status @ WP Dashboard

Managing a WooCommerce store means keeping a close eye on your product pricing, especially when running promotions or sales. However, sorting through hundreds or even thousands of products to find those with active sale prices can be a tedious task using the default WordPress admin interface.

Imagine being able to quickly filter your product list to display only items that are currently on sale, allowing you to streamline your updates and promotional strategies…

In this post, I’ll show you how to enhance your WooCommerce admin panel with a simple PHP snippet that adds a custom filter for sale status.

This solution not only saves you time but also simplifies inventory management, making it easier to plan marketing campaigns and update product details efficiently. Let’s dive into the code and get started!

PHP Snippet: Add a “Filter by sale status” Dropdown @ WooCommerce Products Admin

/**
 * @snippet       Filter products by sale status @ WP admin
 * @how-to        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 9
 * @community     https://businessbloomer.com/club/
 */

add_action( 'restrict_manage_posts', 'bbloomer_filter_products_by_sale_status', 9999 );

function bbloomer_filter_products_by_sale_status() {
    global $typenow;
    if ( 'product' === $typenow ) {
        $selected = isset( $_GET['sale_status'] ) ? $_GET['sale_status'] : '';
        ?>
        <select name="sale_status">
            <option value="">Filter by sale status</option>
            <option value="on_sale" <?php selected( $selected, 'on_sale' ); ?>>On Sale</option>
            <option value="not_on_sale" <?php selected( $selected, 'not_on_sale' ); ?>>Not on Sale</option>
        </select>
        <?php
    }
}

add_filter( 'request', 'bbloomer_filter_products_query_by_sale_status' );

function bbloomer_filter_products_query_by_sale_status( $query_vars ) {
    global $typenow;
    if ( 'product' === $typenow && isset( $_GET['sale_status'] ) && '' !== $_GET['sale_status'] ) {
        $sale_status = sanitize_text_field( wp_unslash( $_GET['sale_status'] ) ); 
        if ( 'on_sale' === $sale_status ) {
            $query_vars['meta_query'][] = array(
                'key'     => '_sale_price',
                'value'   => '',
                'compare' => '!=',
            );
        } elseif ( 'not_on_sale' === $sale_status ) {
            $query_vars['meta_query'][] = array(
                'key'     => '_sale_price',
                'value'   => '',
                'compare' => '=',
            );
        }
    }
    return $query_vars;
}

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

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 *