Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Add a Dropdown “Sort by Discount: High to Low” Filter Option in WooCommerce Shop page

When online stores sell products, they often offer discounts to boost sales. This WooCommerce customization helps you to emphasize discounts more effectively on the shop page. By adding a “Sort by Discount: High to Low” option to the default sorting dropdown, store owners can make their most discounted products more visible to shoppers. This ensures that products with the highest discounts appear at the top, followed by those with lower discounts.

Solution: Add a “Sort by Discount: High to Low” Filter Option in WooCommerce Shop page

The code adds a discount dropdown field on the WooCommerce shop page, allowing users to filter products based on the discount percentage. Also, the code ensures that the products are sorted from high to low discounts.

// Add "Sort by Discount: High to Low" option in the sorting dropdown
function add_custom_discount_sorting_option( $sorting_options ) {
    $sorting_options['discount_high'] = __( 'Sort by Discount: High to Low', 'woocommerce' );
    return $sorting_options;
}
add_filter( 'woocommerce_default_catalog_orderby_options', 'add_custom_discount_sorting_option' );
add_filter( 'woocommerce_catalog_orderby', 'add_custom_discount_sorting_option' );

// Use posts_clauses to dynamically join price meta and sort by discount
function ts_add_discount_sorting_clauses( $clauses ) {
    if ( isset( $_GET['orderby'] ) && 'discount_high' === $_GET['orderby'] ) {
        global $wpdb;

        // Join to fetch the regular and sale prices
        $clauses['join'] .= " 
            LEFT JOIN {$wpdb->postmeta} AS meta_regular 
                ON ({$wpdb->posts}.ID = meta_regular.post_id AND meta_regular.meta_key = '_regular_price')
            LEFT JOIN {$wpdb->postmeta} AS meta_sale 
                ON ({$wpdb->posts}.ID = meta_sale.post_id AND meta_sale.meta_key = '_sale_price')
        ";

        // Use a CASE statement to calculate discount:
        // If sale price exists and is lower than regular price, calculate the discount percentage.
        // Otherwise, return -1 so that non-discounted products are pushed to the bottom.
        $clauses['orderby'] = "
            CASE 
                WHEN meta_sale.meta_value IS NOT NULL 
                     AND meta_sale.meta_value != '' 
                     AND CAST(meta_sale.meta_value AS DECIMAL(10,2)) < CAST(meta_regular.meta_value AS DECIMAL(10,2))
                THEN ((CAST(meta_regular.meta_value AS DECIMAL(10,2)) - CAST(meta_sale.meta_value AS DECIMAL(10,2))) / CAST(meta_regular.meta_value AS DECIMAL(10,2)))
                ELSE -1
            END DESC
        ";
    }
    return $clauses;
}
add_filter( 'posts_clauses', 'ts_add_discount_sorting_clauses' );

Output

Among the default sorting options, you will now see a new option: “Sort by Discount: High to Low,” as shown below.

How to Add a Dropdown "Sort by Discount: High to Low" Filter Option in WooCommerce Shop page - Tyche Softwares

When you select the above custom option, you will notice that products are listed in descending order based on their discount percentage.

Add a Dropdown “Sort by Discount: High to Low” Filter

Looking to make shopping easier for your customers? With our customized solution, you can add the WooCommerce Brands feature to your store, allowing shoppers to filter products by brands on the WooCommerce shop page. This simple yet powerful upgrade helps customers find their favorite brands faster, improving their overall experience. Try it out today and take your store to the next level!

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Newest
Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x