DEV Community

Cover image for Move out of stock items to the end of the loop in WooCommerce
SeanAUS120
SeanAUS120

Posted on • Edited on

Move out of stock items to the end of the loop in WooCommerce

Lot's of times your out of stock items are still bringing in SEO traffic so you want to keep them in your shop. If you have a large store, and products go in and out of stock all the time, it can be a UX nightmare for customers to constantly get shown out-of-stock products in their searches.

The fix is to move the out-of-stock products to the end of the loop, with this custom snippet in your functions.php. We're using this exlusively on Wine N Liquor which has over 15,000 products on the store and hourly stock updates.

add_filter( 'woocommerce_get_catalog_ordering_args', 'seanaus120_first_sort_by_stock_amount', 9999 );

function seanaus120_first_sort_by_stock_amount( $args ) {
   $args['orderby'] = 'meta_value';
   $args['order'] = 'ASC';
   $args['meta_key'] = '_stock_status';
   return $args;
}
Enter fullscreen mode Exit fullscreen mode

Currently in use at:
Wild Posting
Gorilla Printing
Wine N Liquor
Wheatpaste Posters
Wheatpaste Posters
Poster Printing
Poster Printing NYC
OOH Marketing
Million Dollar Club
Gift Ideas
Dark Fibre
Hybrid Fibre-Coaxial
Window Advertising
Window Graphics
Window Wraps
Window Stickers

Top comments (0)