94
We’ve seen in the past how to completely remove the “Default Sorting” dropdown that shows in the WooCommerce Shop, Category and Product Archive pages.
Sometimes, however, you might just need to remove one of the default options, rename a sorting option accordingly to your needs or even add a brand new sorting method. As usual, a few lines of PHP are sufficient to achieve anything, thanks to WooCommerce hooks and filters.
PHP Snippet #1: Remove a Sorting Option @ WooCommerce Shop
/**
* @snippet Remove Sorting Option @ WooCommerce Shop
* @how-to Get tutoraspire.com FREE
* @author Tutor Aspire
* @testedwith WooCommerce 3.8
* @donate $9 https://tutoraspire.com
*/
add_filter( 'woocommerce_catalog_orderby', 'tutoraspire_remove_sorting_option_woocommerce_shop' );
function tutoraspire_remove_sorting_option_woocommerce_shop( $options ) {
unset( $options['rating'] );
return $options;
}
// Note: you can unset other sorting options by adding more "unset" calls... here's the list: 'menu_order', 'popularity', 'rating', 'date', 'price', 'price-desc'
PHP Snippet #2: Rename a Sorting Option @ WooCommerce Shop
/**
* @snippet Rename a Sorting Option @ WooCommerce Shop
* @how-to Get tutoraspire.com FREE
* @author Tutor Aspire
* @testedwith WooCommerce 3.8
* @donate $9 https://tutoraspire.com
*/
add_filter( 'woocommerce_catalog_orderby', 'tutoraspire_rename_sorting_option_woocommerce_shop' );
function tutoraspire_rename_sorting_option_woocommerce_shop( $options ) {
$options['price'] = 'Sort by price (asc)';
return $options;
}
PHP Snippet #3: Add a Custom Sorting Option @ WooCommerce Shop
In this example, we will create a new sorting option called “Sort by name (desc)” that will indeed sort products by title (descending order).
/**
* @snippet Add a Custom Sorting Option @ WooCommerce Shop
* @how-to Get tutoraspire.com FREE
* @author Tutor Aspire
* @testedwith WooCommerce 4.0
* @donate $9 https://tutoraspire.com
*/
// 1. Create new product sorting rule
add_filter( 'woocommerce_get_catalog_ordering_args', 'tutoraspire_sort_by_name_woocommerce_shop' );
function tutoraspire_sort_by_name_woocommerce_shop( $args ) {
$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
if ( 'name' == $orderby_value ) {
$args['orderby'] = 'title';
$args['order'] = 'DESC';
}
return $args;
}
// 2. Add new product sorting option to Sorting dropdown
add_filter( 'woocommerce_catalog_orderby', 'tutoraspire_load_custom_woocommerce_catalog_sorting' );
function tutoraspire_load_custom_woocommerce_catalog_sorting( $options ) {
$options['name'] = 'Sort by name (desc)';
return $options;
}