July 7, 2021 WooCommerce
By default, WooCommerce has some basic sorting options on the shop page that works very well. However, you may want to have some extra sorting options for your WooCommerce store, so customers can easily find specific products they want.
Today, we will show you how to extend WooCommerce default sorting options on the shop page and add your custom sorting options.
Custom Sorting Options
WooCommerce has the following default sorting options:
- Default sorting
- Sort by popularity
- Sort by average rating
- Sort by latest
- Sort by price: low to high
- Sort by price: high to low
We will add the following custom sorting options:
- Sort by popularity: last 30 days
- Sort by popularity: last 60 days
WooCommerce already has a sorting option to sort products by popularity which display popular products by all time. But having extra two sorting options to find popular products, you are giving your customers more flexibility to find popular products for a specific time.
Prepare the Plugin
First, we will create a custom extension plugin for WooCommerce to write our custom PHP code. It’s easy to create a simple starter plugin using the WP CLI command.
Run the following command to generate a plugin with starter code:
Make sure to run the command from the installation of the WordPress root directory.
After the plugin is generated, open the
wc-sorting-options.php file and update the header comment to the following:
Finally, the Sorting Options for WooCommerce plugin is ready to be activated from the WordPress admin dashboard.
Navigate to Plugins -> Installed Plugins and activate the plugin.
Don’t be panic, our plugin does not add any features yet. We will add custom features soon.
The Main Plugin Class
Let’s create our main plugin class where we will write most of our code to add custom sorting options.
Now include this class in our main plugin file with the following code:
Check for Woocommerce and Initialize the Plugin
To initialize the main plugin class you can write the following code in the main plugin file:
You have noticed that we are initializing the class only if the WooCommerce class exists. This ensures that the WooCommerce plugin is installed and activated before initializing our plugin class.
Update Product Sorting Options
Now, it’s time to update product sorting options. We will use
woocommerce_catalog_orderby hook to add our custom sorting options by filtering the default options.
Update the main plugin class with the following code:
If you visit the shop page now, you will be able to see that new options are added to the select dropdown. However, these custom options do not work yet.
Filter the Query
In this step, we will filter the query clauses based on the selected sorting option. Update the main with the following code:
Now, add the missing callback function:
If you take a closer look into the above code, you will see that first, we are preparing the before and after dates. To filter products based on quantity and the specific date range we are using
wc_order_product_lookup table which stores additional data for WC orders.
We are using
product_qty column to get the sum of product quantity of order and
date_created column to filter orders of specific date range. We are using a subquery to prepare
items_sold for our main query.
Finally, if you go to the shop page and try to sort products by Sort by popularity: last 30 days or Sort by popularity: last 60 days you will get popular products based on your selected option.
It’s not just an end, you can more custom sorting options for your WooCommerce store by following the above instructions.