Dean Williams

Change sorting options in WooCommerce

You are currently viewing the Mobile Optimized version (AMP), some features may be missing or may not work as expected. Open Full Version.

WooCommerce is a great plugin for WordPress, enabling you to turn a standard WordPress install into a fully functional eCommerce shop.

However I found there is no options to modify the default sorting options provided in WooCommerce categories, however after some digging I found that you can easily remove options from WooCommerce with some simple code which you can add to your themes functions.php:

 

Remove Sorting Options

// Modify the default WooCommerce orderby dropdown
//
// Options: menu_order, popularity, rating, date, price, price-desc
function my_woocommerce_catalog_orderby( $orderby ) {
unset($orderby["price"]);
unset($orderby["price-desc"]);
unset($orderby["date"]);
unset($orderby["rating"]);
unset($orderby["popularity"]);
unset($orderby["menu_order"]);
return $orderby;
}
add_filter( "woocommerce_catalog_orderby", "my_woocommerce_catalog_orderby", 20 );

the code above includes ability to remove all options, so edit it so that it only removes the options you need to remove. Here is a key to clarify what each option is for:

price - Sort by price: low to high

price-desc - Sort by price: high to low

date - Sort by newness

rating - Sort by average rating

popularity - Sort by popularity

menu_order - Sort by Default sorting

 

Add New Sorting Options

Even more exciting is the ability to add your own sorting options, the example below shows how to add a new option to sort products alphabetically:

//Add Alphabetical sorting option
function my_alphabetical_woocommerce_ordering( $sort_args ) {
$orderby_value = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
if ( 'alphabetical' == $orderby_value ) {
$sort_args['orderby'] = 'title';
$sort_args['order'] = 'asc';
$sort_args['meta_key'] = '';
}
return $sort_args;
}
add_filter( 'woocommerce_get_catalog_ordering_args', 'my_alphabetical_woocommerce_ordering' );
function my_custom_woocommerce_catalog_orderby( $sortby ) {
$sortby['alphabetical'] = 'Sort by name: alphabetical';
return $sortby;
}
//add option to dropdown
add_filter( 'woocommerce_catalog_orderby', 'my_custom_woocommerce_catalog_orderby' );
//add options to admin panel
add_filter( 'woocommerce_default_catalog_orderby_options', 'my_custom_woocommerce_catalog_orderby' );

There you have it! An easy way to modify the sorting options of WooCommerce.

Author: Dean Williams

I'm a Web Developer, Graphics Designer and Gamer, this is my personal site which provides PHP programming advice, hints and tips