We’ve already seen how to add a custom “Product Sorting” option to the “Default Sorting” dropdown in the WooCommerce Shop page.

The task I was presented with, however, was to display items based on a custom “meta key”. Now, if you have no idea what a “meta key” is, don’t worry too much. For example, “_stock_status” is one of these keys, and therefore you can sort products by that key as opposed to product name, date, price, etc.

Anyway, let’s get into coding – as usual, simply copy/paste this little plugin in your functions.php and you’ll get the wanted result 🙂

WooCommerce: sort products by stock (push out of stock products to the end of every WooCommerce loop)

PHP Snippet: Sort Products by Stock Status @ WooCommerce Shop

Note 1: this snippet completely overrides whatever you pick in the “default sorting” dropdown. You will need to remove the WooCommerce sorting dropdown completely with a snippet

Note 2: this snippet sorts products by “stock_status” in “ASC” (ascending) order. Possible values are “instock” “outofstock” “onbackorder” so it will automatically display them alphabetically like this: 1.instock 2.onbackorder 3.outofstock

Note 3: all your products must be using the “Managing Stock” option in the product edit page > Product Data > Inventory. Otherwise I’m not sure what happens!

 * @snippet       Sort Products By Stock Status - WooCommerce Shop
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.9
 * @donate $9     https://businessbloomer.com/bloomer-armada/

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

function bbloomer_first_sort_by_stock_amount( $args ) {
	$args['orderby'] = 'meta_value';
	$args['order'] = 'ASC';
	$args['meta_key'] = '_stock_status';
	return $args;

Leave a Reply