Try to search your question here, if you can't find : Ask Any Question Now ?

Show out of stock products at the bottom preserving products order

HomeCategory: stackoverflowShow out of stock products at the bottom preserving products order
bhawya asked 2 weeks ago

I’d like to place out of stock products in Woocommerce catergory page at the bottom of the list yet I want to preserve initial user’s order.
So for example, if a customer selects “order by price”, it should order by price products and show “In stock” products at the top of the list, and then order by price and show at the bottom out of stock products. So it’ll some sort of a separate query.
Currently my code looks like this:

add_action( 'woocommerce_product_query', 'sort_by_stock_status', 999 );

function sort_by_stock_status( $query ) {
    if ( is_admin() ) return;

    $meta_query = $query->get('meta_query');
    $orderby = $query->get('orderby');


    $meta_query['stock_status'] = array('key' => '_stock_status', 'compare' => 'EXISTS');
    $query->set('meta_query', $meta_query);

    $orderby['stock_status'] = 'ASC';
    $query->set('orderby', $orderby);

}

So the clue is it will firstly perform user’s ordering, and the the stock ordering which added by the code. But the code for some reason doesn’t work.

1 Answers
Best Answer
Arben answered 2 weeks ago
Your Answer

14 + 17 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com