WooCommerce Sort Products By Stock Status

WooCommerce product archive showing in stock products first and custom sorting option

Pre-Requisites

There are no pre-requisites in order for you to be able to implement this solution.

How To Implement This Solution?

Simply copy the pre-coded solution to your active theme’s functions.php or preferably the Code Snippets Plugin.

About this Solution

This quick guide shows you how you can sort products on the archive template by stock status. This means you’ll be able to show In Stock products first, improving the customer experience.

				
					/**
 * Snippet Name:	WooCommerce Sort Products By Stock Status
 * Snippet Author:	ecommercehints.com
 */

add_filter('woocommerce_catalog_orderby', 'ecommercehints_sort_products_by_stock');
function ecommercehints_sort_products_by_stock($options) {
	$options['in-stock'] = 'Show in stock products first';
	return $options;

}

add_filter('woocommerce_get_catalog_ordering_args', 'ecommercehints_sort_products_by_stock_order');
function ecommercehints_sort_products_by_stock_order($args) {
	if( isset( $_GET['orderby'] ) && 'in-stock' === $_GET['orderby'] ) {
		$args['meta_key'] = '_stock_status';
		$args['orderby'] = array( 'meta_value' => 'ASC' ); // Show products in stock first. Change 'ASC' to 'DESC' to show out of stock products first.
	}
	return $args;
}
				
			

Snippet Benefits

  • Improved customer experience – show products in stock first preventing pointless scrolling.
  • Alternatively, show out of stock products first to let customers know your products are in high demand.
WooCommerce Conversion Rate Optimisation (CRO) eBook
100 WooCommerce Conversion Rate Optimisation Tips

Leave a Reply

If you are going to write code in the comments, please wrap it between code tags.

Your email address will not be published. Required fields are marked *

Other Recent Guides

Subscribe To Emails

Get exclusive WooCommerce tips that I only share with email subscribers

Join hundreds of other subscribers