Sort by Discount: How to add Discount Based Sorting in Product Archive Filter

Posted by
Spread the love

There are default sorting options in WooCommerce archive page and those are : Sort by popularity, Sort by average rating, Sort by latest, Sort by price in acsending and descending order. But sometimes, these ones are not enough. In my previous experience of work I found many of clients asking me to proivde an option so that they can sort the products by discount. And that’s why I finally made up my mind to share it with you guyz.

//This is hot deals , sort by discount
function custom_woocommerce_catalog_orderby( $sortby ) {
	$sortby['nd-discount'] = __( 'Sort by Discount', 'woocommerce' );
	return $sortby;
}
add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby', 20 );

// Add filter to modify the SQL query
add_filter('posts_clauses', 'nd_handle_discount_percentage_posts_clauses', 10, 2);
function nd_handle_discount_percentage_posts_clauses( $clauses, $query ) {
	if ( ! is_main_query() || ! isset( $_GET['orderby'] ) || $_GET['orderby'] != 'nd-discount' || $query->get('post_type') != 'product' ) return $clauses;
	global $wpdb;
	$clauses['join'] .= "
            INNER JOIN {$wpdb->postmeta} pm ON $wpdb->posts.ID = pm.post_id AND pm.meta_key = '_regular_price'
    INNER JOIN {$wpdb->postmeta} discount ON $wpdb->posts.ID = discount.post_id AND discount.meta_key = '_sale_price'
        ";
	$clauses['where'] .= $wpdb->prepare(" AND $wpdb->posts.post_type = 'product'
    AND $wpdb->posts.post_status = 'publish'
    AND pm.meta_value != ''
    AND discount.meta_value != ''
    AND ( ( CAST(pm.meta_value AS DECIMAL) - CAST(discount.meta_value AS DECIMAL) ) / CAST(pm.meta_value AS DECIMAL)) >= ( %d / %d )
    AND ( ( CAST(pm.meta_value AS DECIMAL) - CAST(discount.meta_value AS DECIMAL) ) / CAST(pm.meta_value AS DECIMAL)) <= ( %d / %d )"
		,
		1,
		100,
		100,
		100
	);

	// Add the ORDER BY clause
	$clauses['orderby'] = "( ( CAST(pm.meta_value AS DECIMAL) - CAST(discount.meta_value AS DECIMAL) ) / CAST(pm.meta_value AS DECIMAL)) DESC";
	remove_filter('posts_clauses', 'flatsome_handle_discount_percentage_posts_clauses', 10);
	return $clauses;
}

With the code in ‘woocommerce_catalog_orderby’ filter we are adding the sorting option in Sorting filter dropdown.

With the code in “posts_clauses” filter, we are making raw query to get the product by discount.

Where to place the code

You can place it in a custom plugin or theme’s function.php file.

Or, you can download this plugin. Here the whole code is provided as individual WordPress plugin. You can download and install it in your project.

Let me know if it is serving your purpose. Feel free to knock me and share your though about anything.
Wanna learn more exciting things like this ? Join us by subscribing in the form below.

About The Author

Thank you for reading this post, don't forget to subscribe!

Choose your interests. You will get future contents from the topic you select.

Bangla
Career
Coding tips & tricks
Database
English
Laravel
PHP
Programming
Uncategorized
WordPress
খাদ্যাভ্যাস
চিন্তন
প্যারেন্টিং
বিজনেস
স্টোরি

Leave a Reply

Your email address will not be published. Required fields are marked *