Different Default Sorting per Category
Sets the default product order for specific categories, for example newest first for "New arrivals" and price for "Clearance", while others keep the store default.
Opens WPIDE on
Code
$wpide_category_sorting = array( // Category slug => default sorting.
'new-arrivals' => 'date',
'clearance' => 'price',
);
add_filter( 'woocommerce_default_catalog_orderby', function ( $orderby ) use ( $wpide_category_sorting ) {
if ( ! is_product_category() ) {
return $orderby;
}
$term = get_queried_object();
return ( $term && isset( $wpide_category_sorting[ $term->slug ] ) ) ? $wpide_category_sorting[ $term->slug ] : $orderby;
} );
Notes
Values are the sorting keys WooCommerce uses: menu_order, popularity, rating, date, price, price-desc.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.