Are you a WooCommerce store owner looking to showcase your sale items in a more dynamic way? Perhaps you’ve noticed that the default sorting options just aren’t cutting it for your needs.
You’re not alone! Many store owners face the challenge of wanting to sort sale items by the date they were last modified, rather than the date they were published.
But fear not, we’re here to help you solve this problem and take your store to the next level!
The Problem:
By default, WooCommerce provides sorting options for sale items based on the date they were published. However, for many store owners, this isn’t ideal. You want your customers to see the latest updates to your products, whether it’s a price change or a new feature. But without the ability to sort by modified date, you’re left feeling frustrated and limited in your options.
The Solution:
Luckily, there’s a simple solution to this problem that doesn’t require you to be a coding expert. By making a few adjustments to your WooCommerce settings and adding a custom shortcode, you can easily sort your sale items by modified date.
Step-by-Step Instructions:
1. Backup Your Website:
Before making any changes to your WooCommerce settings or code, it’s essential to create a backup of your website. This ensures that if anything goes wrong during the process, you can easily restore your site to its previous state.
2. Use a Child Theme:
If you’re planning to make code changes to your theme files, it’s highly recommended to use a child theme. This protects your modifications from being overwritten when you update your parent theme. Creating a child theme is simple and ensures that your customizations remain intact.
3. Add Custom Sorting Option:
To add the ability to sort sale items by modified date, we’ll need to add a custom sorting option to your WooCommerce settings. This can be done by adding a snippet of code to your theme’s functions.php file or by using a plugin that provides this functionality.
4. Implement Custom Shortcode:
Once the custom sorting option is added, we’ll create a custom shortcode that utilizes this sorting method. This shortcode will allow you to display your sale items sorted by modified date on any page of your website.
5. Test and Verify:
After implementing the changes, it’s crucial to thoroughly test the functionality to ensure everything is working as expected. Check different scenarios, such as sorting by modified date and verifying that sale items are displayed correctly.
Code Snippet
Below is a sample code snippet that adds the custom sorting option and implements the custom shortcode for sorting sale items by modified date.
Copy & paste the below code at the bottom of your theme’s (child theme) functions.php
// Add custom sorting option
function custom_woocommerce_catalog_orderby( $sortby ) {
$sortby['modified'] = __( 'Sort by modified date', 'woocommerce' );
return $sortby;
}
add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby', 20 );
// Modify the shortcode to sort by modified date
add_filter( 'woocommerce_shortcode_products_query', 'custom_modify_products_query', 10, 3 );
function custom_modify_products_query( $query_args, $attributes, $type ) {
if ( isset( $attributes['orderby'] ) && 'modified' === $attributes['orderby'] ) {
$query_args['orderby'] = 'modified';
}
return $query_args;
}
Short code
Copy and paste the below code into any shortcode widget (Gutenberg, Elementor, Spectra etc.) on a page and modify the values according to your requirement.
[products limit="8" columns="4" class="quick-sale" on_sale="true" orderby="modified" order="desc"]
Conclusion:
With these simple steps, you can now easily sort your WooCommerce sale items by modified date, providing your customers with the latest updates and improvements to your products.
Remember to always backup your website before making any changes and consider using a child theme to protect your customizations.
By implementing these changes, you’ll enhance the user experience on your store and stay ahead of the competition. Happy selling!