Product Archive

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.

100 WooCommerce Conversion Rate Optimisation Tips

This field is for validation purposes and should be left unchanged.

Let’s collaborate!

Need to outsource WordPress development?

Join forces with UnlimitedWP for an outsourced white label web development service you can truly rely on.

First Timer Here?

Sign up to receive 20% off on your first month with us.

26027
WELCOME OFFER