WooCommerce: How to add a ‘Sort By Sales Items First’ shop filter

Do you need to add an option to the default WooCommerce product sort filter to allow the ordering of sales items first? Luckily, this is easily done with a very simple function. You will need some basic knowledge of PHP and how to use functions correctly within a child theme or with a plugin such as Code Snippets.

Sorting Sales Items in WooCommerce

As you can see in the screenshot below the function will add an extra option to the WooCommerce ‘Sort by’ filter, this is dependant on the fact that you have a sorting filter as some themes may have removed this in place of a sidebar filter for example. If you still have the standard WooCommerce sorting filter then we can simply begin by using the function below to add the option to sort sales items above the regular priced stock.

Woocommerce Sort By Sales Items

 

Function to add sorting of sales items

You can use the function below without any changes to achieve what we want in this guide but if you want to change text of the filter option you can do.

/**
 * WooCommerce Sales Sorting Filter
 * https://lakewood.media/woocommerce-add-sales-filter/
 */
add_filter( 'woocommerce_get_catalog_ordering_args', 'wcs_get_catalog_ordering_args' );
function wcs_get_catalog_ordering_args( $args ) {
	$orderby_value = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
	
	if ( 'on_sale' == $orderby_value ) {
	    $args['orderby'] = 'meta_value_num';
	    $args['order'] = 'DESC';
	    $args['meta_key'] = '_sale_price'; 
	}
	return $args;
}

add_filter( 'woocommerce_default_catalog_orderby_options', 'wcs_catalog_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'wcs_catalog_orderby' );
function wcs_catalog_orderby( $sortby ) {
    $sortby['on_sale'] = 'Sort by on sale';
    return $sortby;
}

How to use the filter as a link to your sales items?

This is pretty simple, you could place a menu link to your sales items pretty easily by simply adding the URL string to the end of your shop slug such as https://yourshop.com/products/?orderby=on_sale.

For more help with WooCommerce development visit our WordPress development services page.

Written By
Adam
Founder of WP Helper.
You will also like these articles

Relax Knowing Your WordPress Site Is Secure & Running Smoothly 24/7

Let us manage your WordPress site, everything from security to updates will be taken care of. Support plans also come with dedicated support so we can do anything from adding content to customising your site for you.

Same Day Professional WordPress Support

Get WordPress Support Today

Need help with a single WordPress problem, today? We can help with anything from adding analytics tracking code to site hack recovery. Full money back guarantee on all jobs.

1. Submit a Support Request

Use our support ticket form below to send details of your problem to our developers.

2. Get a Quote

We will review your request and provide a quote within 24 hours (but usually within a few hours).

3. We fix your WordPress problem

Our team will begin fixing your WordPress problem the same day.

4. 100% Money Back Guarantee

If we can’t fix the problem for the amount quoted we will refund you 100%.

  • Do you have a screenshot of the issue or have a copy of the theme or plugin that is at fault? If you want to upload php, html or css please zip first.
    Drop files here or
    Accepted file types: jpg, jpeg, png, pdf, zip, gzip, rar, doc, txt, Max. file size: 15 MB, Max. files: 10.