I want to create a new sorting on Woo which sort like this:
Product 1: stock_status => instock, price => 10;
Product 2: stock_status => outofstock, price => 20;
Product 3: stock_status => outofstock, price => 30;
Product 4: stock_status => instock, price => 40;
New sorting result:
Product 1: stock_status => instock, price => 10;
Product 4: stock_status => instock, price => 40;
Product 2: stock_status => outofstock, price => 20;
Product 3: stock_status => outofstock, price => 30;
First by stock and then by price. I have this working but I don't know how to continue:
add_filter( 'woocommerce_catalog_orderby', 'misha_add_custom_sorting_options' );
function misha_add_custom_sorting_options( $options ){
$options['in-stock'] = 'Show products in stock first';
return $options;
}
add_filter( 'woocommerce_get_catalog_ordering_args', 'misha_custom_product_sorting' );
function misha_custom_product_sorting( $args ) {
// Show products in stock first
if( isset( $_GET['orderby'] ) && 'in-stock' === $_GET['orderby'] ) {
$args['meta_key'] = '_stock_status';
$args['orderby'] = array( 'meta_value' => 'ASC' );
}
return $args;
}
Any help will be appreciate it.