Multiple order by in WooCommerce



PHP Snippet 1:

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'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
    if ( 'sort_by_type' == $orderby_value ) {
        $args['orderby'] = 'meta_value_num title';
        $args['order'] = 'ASC';
        $args['meta_key'] = 'sort_by_type';

    }

    if ( '_wc_average_rating' == $orderby_value ) {
        $args['orderby'] = 'meta_value_num title';
        $args['order'] = 'DESC';
        $args['meta_key'] = '_wc_average_rating';
    }

    return $args;
}

PHP Snippet 2:

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'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
if ( 'sort_by_date' == $orderby_value ) {
$args['orderby'] = 'meta_value';
$args['order'] = 'ASC';
$args['meta_key'] = 'sort_by_date';

}
if ( '_stock_status' == $orderby_value ) {
$args['orderby'] = 'meta_value';
 $args['order'] = 'DESC';
$args['meta_key'] = '_stock_status';
}
 return $args;
}