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.