How to Show Out of stock products at the end in WooCommerce

How to Show Out of stock products at the end in WooCommerce

WooCommerce has a feature that allows you to manage your stock availability. For example, the inventory of some of your goods is meager, and naturally, after the purchase, their inventory will be zero. But, how to show out-of-stock products at the end in Woocommerce.

In this case, to improve the quality of work with your online store, it is better to display non-existent products at the end of your product list. And show the In-Stock products at the beginning of the list.

Stock management in WooCommerce

To manage the inventory of your products, enter the products section and edit one of the products on your website.

Now go to the product data section and activate inventory management from the inventory list section.

Enter your inventory here. If you enter a value of 50, after 50 units of this product are sold, the inventory is displayed as out of stock. In such cases, you must show these products at the bottom of the list of products on your website.

Show Out of stock products at the end in Woocommerce

Once the inventory is zero, we have a variable called _stock_status in WooCommerce that will change this non-existent state. In such cases, we can display the products at the bottom of the list based on the lack of inventory.

Go to Themes> Theme Editor to display non-existent products at the bottom of the product list; go to Themes > Theme Editor.

Then open the functions.php file or theme functions from the left panel.

In the second line of this file, enter the following code and save the file.

add_filter( 'woocommerce_get_catalog_ordering_args', 'mihanwp_sort_by_stock', 9999 );
 
function mihanwp_sort_by_stock( $args ) {
   $args['orderby'] = 'meta_value';
   $args['order'] = 'ASC';
   $args['meta_key'] = '_stock_status';
   return $args;
}

If this file could not be changed through the WordPress admin panel, see the tutorial on changing the WordPress template code.

Just as easily! WordPress was instructed to display the non-existent products at the bottom of the product list by entering the code. 🙂

Good luck. And be happy. 😉

Is there any particular point that would complete this article?

  1. Guest User February 28, 2022

    Thank You! Managed to get this to work however I realized that by placing the out-of-stock products last, for some reason the entire order got moved around. The solution I am looking for is to have out-of-stock products appear last but, have all of the most recent products appear first. Is there a way to make this possible?

    • Reza Rad Reza Rad March 4, 2022

      Your welcome. I think you can’t do that with this filter!