I need to modify 'Popularity' sort option on the product category. By default it is sorting by 'total_sales', but I need to add numeric custom field together with 'total_sales'.
Started first sorting only by custom field under the Popularity option:
add_filter( 'woocommerce_get_catalog_ordering_args', 'wc_add_catalog_orderby_args' );
function wc_add_catalog_orderby_args( $args ) {
$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
switch( $orderby_value ) {
case 'popularity':
WC()->query->remove_ordering_args(); // first remove default Popularity sort
$args = array(
'meta_key' => 'my_custom_field',
'orderby' => 'meta_value_num',
'order' => 'desc'
),
);
break;
}
return $args;
}
And with this one sort parameter it works. But I need to sort with 2 meta keys and I tried with making a meta_query:
add_filter( 'woocommerce_get_catalog_ordering_args', 'wc_add_catalog_orderby_args' );
function wc_add_catalog_orderby_args( $args ) {
$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
switch( $orderby_value ) {
case 'popularity':
WC()->query->remove_ordering_args(); // first remove default Popularity sort
$args = array(
'relation' => 'OR',
//'relation' => 'AND',
'custom_field' => array(
'key' => 'my_custom_field',
'order' => 'meta_value_num'
),
'product_total_sales' => array(
'key' => 'total_sales',
'order' => 'meta_value_num'
),
'orderby' => array(
'my_custom_field' => 'desc',
'product_total_sales' => 'desc',
),
);
break;
}
return $args;
}
And this didn't worked. If any anyone have suggestion I would be very grateful, coffee on me