How to Show Out of Stock Products at the end of a Collection in WooCommerce

0

If you want to show your in stock products before the out of stock products, please use this code below.

/**
* Sorts the Woocommerce Archive product query to push out of stock products to the end
*/
function _nok_order_by_stock_status( $posts_clauses, $query ) {

// only change query on WooCommerce loops
if ( $query->is_main_query() && ( is_product_category() || is_product_tag() || is_product_taxonomy() || is_shop() ) ) {
global $wpdb;

$posts_clauses[‘join’] .=
” LEFT JOIN (
SELECT post_id, meta_id, meta_value FROM $wpdb->postmeta
WHERE meta_key = ‘_stock_status’ AND meta_value <> ”
) istockstatus ON ($wpdb->posts.ID = istockstatus.post_id) “;

$posts_clauses[‘orderby’] =
” CASE istockstatus.meta_value WHEN
‘outofstock’ THEN 1
ELSE 0
END ASC, ” . $posts_clauses[‘orderby’];
}

return $posts_clauses;
}
add_filter( ‘posts_clauses’, ‘_nok_order_by_stock_status’, 2000, 2 );

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here