For WooCommerce store owners, providing an intuitive shopping experience is key to customer satisfaction and retention. One effective way to improve user experience is by customising the product sorting options available on your shop page. In this tutorial, we’ll walk through how to customise product sorting options, allowing customers to sort products based on criteria that are most relevant to your shop.
Prerequisites
Ensure you have administrative access to your WordPress site and that you’re familiar with editing PHP files. Consider making these changes in a child theme to preserve them during updates.
Step 1: Define New Sorting Options
Let’s start by adding a new sorting option that allows customers to sort products by popularity within a specific category. This is particularly useful if you have diverse categories with varying degrees of popularity.
Step 2: Add Custom Code to Your Theme’s functions.php
File
Open your theme’s functions.php
file and insert the following PHP code to customise product sorting options. This code snippet adds a new sorting option and defines how products will be sorted when this option is selected.
/*
* Snippet: Customise Product Sorting Options in WooCommerce – 2024
* Author: John Cook
* URL: https://wcsuccessacademy.com/?962
* Tested with WooCommerce 8.8.2
*/
// Add new custom sorting options
add_filter('woocommerce_catalog_orderby', 'wcsuccess_add_custom_sorting_options');
function wcsuccess_add_custom_sorting_options( $options ) {
$options['popularity_category'] = __('Sort by popularity within category', 'woocommerce');
return $options;
}
// Apply custom sorting method based on the new option
add_action('woocommerce_product_query', 'wcsuccess_apply_custom_sorting');
function wcsuccess_apply_custom_sorting( $query ) {
if ('popularity_category' === $query->get('orderby')) {
$query->set('meta_key', 'total_sales');
$query->set('orderby', 'meta_value_num');
$query->set('order', 'desc');
}
}
Explanation of the Code
- Custom Sorting Option: The
wcsuccess_add_custom_sorting_options
function adds a new sorting option to the dropdown menu on the WooCommerce shop page. The option is labelled as “Sort by popularity within category.” - Sorting Logic: The
wcsuccess_apply_custom_sorting
function defines what happens when the new sorting option is selected. It sorts products based on their sales data, displaying the most popular items first. - Meta Key Usage: The use of
meta_key
for sorting bytotal_sales
ensures that products are sorted based on their actual sales performance.
Step 3: Test the New Sorting Option
After implementing the custom code:
- Visit your WooCommerce shop page.
- Use the sorting dropdown to select the new “Sort by popularity within category” option.
- Verify that products are sorted according to their sales figures, showing best-sellers first within any given category.
Customising Further
You can customise this further by adding more conditions or sorting based on other criteria like stock status, date added, or custom attributes. Each additional sorting logic will enhance how customers interact with your product listings, making your store more user-friendly.
Conclusion
Customising product sorting options in WooCommerce allows you to tailor the shopping experience to meet both the business and customer needs. By implementing “Customise Product Sorting Options”, you provide your customers with a more relevant and practical way to browse products, which can lead to increased satisfaction and sales.
Always ensure to test any changes on a staging site before applying them to your live site to prevent any disruptions in the customer shopping experience. This tutorial on how to customise product sorting options should help you get started on making your WooCommerce store even more successful and customer-centric.
I have been working with WordPress and WooCommerce since 2012 and have developed a deep knowledge of the content management system. Since 2012, I have developed several plugins and designed dozens of websites utilising different frameworks, CMS’s and programming languages. I am proficient in PHP, Python, Java, C, C++, R and JavaScript with limited experience in Go, Kotlin and Swift.
Educationally, I have a Master’s degree in cyber security a Bachelor’s (Hons, First Class) in Applied Research and a Graduate Certificate in Data Science. I’m currently undertaking PhD studies investigating IoT cybersecurity. I recently graduated with First Class Honours and Masters of Information Technology, receiving the Executive Dean’s Award for studies undertaken in the 2021 and 2022 academic years. I have worked in the information technology industry for the past 11 years primarily as a software/web developer specific to design, optimisation, network management and security. My research interests are in the areas of Internet of Things (IoT), 5G and Beyond Networks, information security for wireless networks and software development.
Google Scholar ID: https://scholar.google.com.au/citations?user=0KvpwNEAAAAJ&hl=en
ORCID: https://orcid.org/0000-0001-8113-1296
Stay In Touch