I'm adding extra sorting options for products, like by sku, or stock status. It works, but I would like to have a sorting result by multiple meta keys: all the products in stock must appear before the out of stock ones, but I need them sorted by sku too.
I've found several functions both using woocommerce_get_catalog_ordering_args and woocommerce_product_query, but none of them seems working. I only get a sorting by _sku OR by _stock_status, never combined.
Any code working with the latest woocommerce version?
actual code:
add_filter('woocommerce_get_catalog_ordering_args', 'sort_products_by_stock_order');
function sort_products_by_stock_order($args) {
// in-stock filter and default
if(!isset( $_GET['orderby'] ) || ( isset( $_GET['orderby'] ) && 'in-stock' === $_GET['orderby'] )) {
$args['meta_key'] = '_stock_status';
$args['orderby'] = array( 'meta_value' => 'ASC' );
// SORT BY SKU
} else if( isset( $_GET['orderby'] ) && 'sku' === $_GET['orderby'] ) {
$args['meta_key'] = '_sku';
$args['orderby'] = array( 'meta_value' => 'ASC' );
}
return $args;
}
Thanks