Add WooCommerce Custom sorting option by category

47 Views Asked by At

I am trying to arrange/group products shown on a WooCommerce category page by primary category when they are in multiple categories.

So, for example:

  • Products A, B and C are in Category 1 (primary) and also Category 2
  • Products X, Y and Z are in Category 3 (primary) and also Category 2
  • When I view shop Category 2 page, I want to display A,B,C together and X,Y,Z next to each other

This was my starting point for the code, taken from another suggestion to randomise the order; I understand that it's not as simple as adding $args['orderby'] = 'category' but am unsure how to achieve this:

add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args' );
function custom_woocommerce_get_catalog_ordering_args( $args ) {
  $orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
    if ( 'category_list' == $orderby_value ) {
        $args['orderby'] = 'category';  // I realise this doesn't work like this...
        $args['order'] = '';
        $args['meta_key'] = '';
    }
    return $args;
}
add_filter( 'woocommerce_default_catalog_orderby_options', 'custom_woocommerce_catalog_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby' );
function custom_woocommerce_catalog_orderby( $sortby ) {
    $sortby['category_list'] = 'Category';
    return $sortby;
}

My Plan B is to just use the default ordering by name/menu order and set all products in each primary category to be the same menu order value but that will potentially be time-consuming both initially and another admin task to do for each new product:

Existing Menu Order facility in WooCommerce products

0

There are 0 best solutions below