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. 😉

How useful was this post?

Click on a star to rate it!

Average rating 3.5 / 5. Vote count: 2

No votes so far! Be the first to rate this post.

What do you think about this?

  1. U309733 ۲۸ آبان ۱۴۰۰

    Hi again. Did that and I think it worked. What might I need to change in my template in order for this to work properly then?

    • Reza Rad Reza Rad ۳۰ آبان ۱۴۰۰

      Your template has not used the standard WordPress hooks and queries.

  2. U309733 ۲۷ آبان ۱۴۰۰

    Hi! I’ve used this snippet of code on my child-theme’s functions.php as per our instructions but nothing happened on my website

    All out-of-stock products still show first.

    I have my default order set to “lower to higher price”. Could that influence the outcome?

    • Reza Rad Reza Rad ۲۷ آبان ۱۴۰۰

      Hi, please make sure your child theme is working properly. To test, you can add a snippet to the main theme’s functions.php file.

      • U309733 ۲۷ آبان ۱۴۰۰

        Hi Reza, thank you for your reply! I did add it to my theme’s functions.php and I it still didn’t work 🙁 too bad because this is actually a really helpfull snippet I’d like to implement.

        • Reza Rad Reza Rad ۲۸ آبان ۱۴۰۰

          Hi again. We tested the snippet and it’s working. Please change your theme and try again.