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.
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.
Adam
Editor of the Lakewood Journal and the founder of Lakewood media. Also an avid landscape and travel photographer.
You may also like
How to fix Webmaster Console crawl errors in WordPress
How to easily fix Google Webmaster Console Errors on your WordPress site using a simple, free plugin. Improve your WordPress site SEO.
0 Comments3 Minutes
How to Improve Your Online Presence
3 steps to improve your digital presence. Whether your business is based online or not, all companies need to have some online presence in the digital age.
0 Comments4 Minutes
How to Hunt for Affordable WordPress Consultancy Services and Actually Hire One
Learn how to find WordPress consultancy services on Google, LinkedIn, and startup directories like Beta List and Product Hunt.
0 Comments14 Minutes
How to create Legal Pages for your WordPress website
Learn how to create Legal Pages in WordPress, use manual approach, WordPress plugins, or hire a Lawyer to create Legal Pages for your WordPress site.
0 Comments13 Minutes
How to Design a Site Experience Users Will Love
It's no secret that a website is basically the face of the company. It is the representation of its story and mission, as well as the company's main point of sale, in most cases.
0 Comments12 Minutes
How to optimise and compress your images
A quick guide to optimising, compressing and resizing your images on your Mac or Windows computer before uploading to your website.
0 Comments8 Minutes