Change default sorting for a specific Woocommerce product category archive pages

WooCommerce only has a setting to change the sort order of all product archive pages, occasionally you may want to change default sorting for a specific Woocommerce product category archive pages.  For example your store may be set to show products by Popularity by default, however, you have a “New Arrivals” category that you’d want to sort by date. Or a “Sale” category you want to sort by price.

add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_catalog_ordering_args', 20, 1 );
function custom_catalog_ordering_args( $args ) {
  $product_category = 'new-arrivals'; // <== 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

  //if( $args['meta_key'] )
  //  unset($args['meta_key']);

  return $args;
}

There is an optional, this was added to remove the meta key set by some sorting options. Enable this if you are having problems.

if( $args['meta_key'] )
    unset($args['meta_key']);

Originally from StackOverflow


2 Comments

  • Dan

    July 15, 2019

    Whereabouts do I put this code?

    Reply
    • LWD

      July 15, 2019

      You’ll need to add this into your theme’s functions.php file

      Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.