Hello I am trying to change the default sorting order for one specific woocommerce category.
This category has a slug of cbd-best-sellers
I am trying to change the default sort order to "by popularity" for this category.
I found this below code which changes the default sorting to "by date" for a specific category
add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_catalog_ordering_args', 20, 1 );
function custom_catalog_ordering_args( $args ) {
$product_category = 't-shirts'; // <== HERE define your product category
// Only for defined product category archive page
if( ! is_product_category($product_category) ) return $args;
// Set default ordering to 'date ID', so "Newness"
$args['orderby'] = 'date ID';
if( $args['orderby'] == 'date ID' )
$args['order'] = 'DESC'; // Set order by DESC
return $args;
}
I than replaced t-shirts with my category slug cbd-best-sellers and changed date ID to popularity like so:
add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_catalog_ordering_args', 20, 1 );
function custom_catalog_ordering_args( $args ) {
$product_category = 'cbd-best-sellers'; // <== HERE define your product category
if( ! is_product_category($product_category) ) return $args;
$args['orderby'] = 'popularity';
if( $args['orderby'] == 'popularity' )
$args['order'] = 'ASC'; // Set order by DESC
return $args;
}
But the category is still not sorting by popularity on load.
Am i doing it wrong?
the hook you are using is for adding or changing the order by values not to set the default one.
If you want to set default sorting option you need to use
woocommerce_default_catalog_orderby
so your code should be like the following: