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.
So, let’s see how to show all out of stock products as last in the shop, category and loop pages. As usual, simply copy/paste this little plugin in your functions.php and you’ll get the wanted result ๐
PHP Snippet: Sort Products by Stock Status @ WooCommerce Shop
Note 1: this snippet only works when you use “Default Sorting” from the WordPress > Customize > WooCommerce > Product Catalog > Default product sorting section. It won’t override sorting by price / popularity / etc. when these are set as default.
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 5
* @community https://businessbloomer.com/club/
*/
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['meta_key'] = '_stock_status';
return $args;
}
Hi,
the code works perfectly, but not for the search results, I also put ( is_search() ) but it doesn’t work, can you help me please?
Thank you
Hello Domenico, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hello, thank you for this! It works great. However, I was wondering if theres a way to have it work on Sort by Price and Sort by Popularity as well?
Hello Jessica, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Unfortunately, this snippet breaks the “products” shortcode for displaying new products โ the table of new products is not updated (or updated incorrectly) if the snippet is included.
I guess that’s possible, sorry about that
Hi. Works fine, but with the exception that the order was descending. I had to add ‘order’ = ‘ASC’ for it to work as described.
Nice!
Hello,
How to do that? What to add in the code below?
“Hi. Works fine, but with the exception that the order was descending. I had to add โorderโ = โASCโ for it to work as described.”
Thanks!
Best regards
Hi,
This works great, however my client wants all in stock items to show first, but they also want the menu order setting to be respected for in stock items, is that possible?
Hi Bob, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hi! I’ve tried your code but it didn’t work for me on my theme. I worked on standard WP themes to. What could I have in my theme that prevents this from working?
I also have my default ordering from minimum to maximum price and an exception for a couple of pages inserted via code in the functions.php file.
Not sure, this require custom troubleshooting so I can’t help here. Did you try contacting the theme developers?
Hi, Code working well.
Please consider create similar code on your blog but for products with images first . ๐
Thanks – I’ll see what I can do
Can i use this snippet for sorting product variations?
On the single product page? Nope
The Code worked well for us so thank you ๐
But we have two shoes that doesn’t follow, not sure how that can be?
As well if we want to add it to sort it on type, how would we do that?
Thanks in advance
Hi Christian, thanks so much for your comment! Yes, this is definitely possible, but Iโm afraid itโs custom troubleshooting work. If youโd like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
For Information Only
Strangely this did’nt work for me in WC 4.9.2 but Works in WC 5.0.0
Thanks Rodolfo
Update, It didnt work because site had “Post Types Order” plugin installed, after I deactivated that the snippet worked perfectly.
Ah cool
Hi, nice trick
Is there a way for this to work on shortcodes? I notice that it only works in shop page.
Hi Josรจ, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hi your code works but does not take into account external / affiliate products in the order. Is there a way to see these products first and then eventually the out of stock products?
Hi Alonso, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Works beautifully – Let me a buy you a beer!
Cool! Here’s a $9 beer for you: https://www.businessbloomer.com/bloomer-armada/#pricing
Works great! Thanks. Is it possible to give green Color to the text?
Thanks in advance
Thanks! You can achieve that with simple CSS
hi, how can order products by stock _status and date??
Hey Jerax, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hi,
there is a possibility to add first only product by some user id ?
Thanks
Hi Marco, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
hi
how to show just in stock product in related product section?
Hi, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
How to make this as by default order
Hi Anil, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Snippet 1 works well ! thank you !!
Great!
it doesn’t work for me.
what should i do?
To troubleshoot, disable all plugins but WooCommerce and also switch temporarily to “Twentyseventeen” theme (load the snippet there in functions.php) – does it work? If yes, you have a problem with your current theme or one of the plugins.
Hope this helps!
R
Hello and thank you for this function.
If someone need to sort by stock status + other criteria un can try this changed code:
Just change menu_order with your criteria…
Nice!
this code created http 500 error for me
You’re right, I’ve fixed it now for you. Let me know
How would you amend this code to display products on backorder first, then in- and out- of stock?
Hello Greg, this will automatically display them alphabetically like this: 1.instock 2.onbackorder 3.outofstock so we need to find a different workaround. This is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hi,
I’m sorry about my English.
I used your code and it works correctly.
But now I have to change the order.
Is there a possibility to order products for the best sellers and put out of stock products at the bottom of the page?
Thank you
Mirco
Hi Mirco, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Good day.
I do everything according to your instructions.
But I can not. It is necessary that the goods be sorted by quantity in stock. First of all, cho would show the goods of which are more in stock.
Help please solve this problem.
WordPress 5.2
WooCommerce 3.6.2
Hi Roman, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Great snippet. How about one which sorts by in stock variables on the single product page ?? So when you 1st land on the page the first available in stock item is ready to buy instead of if the default item is out of stock?
Hello Wayne, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hi there. We like this code and want to use it, but we also want to enable it so that ‘stock’ sorts it first by stock, then by title.
So the page loads in Title order, but with ‘out of stock’ at the end of the pagination.
I don’t see a way to do this, as ‘title’ isn’t a meta_value, like price and SKU are.
Can you help?
Hey Simon, thanks so much for your comment! Yes, this is possible – unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R
Hi Rodolfo,
Thank you again for all your work and support to the community.
This code works wells. However it messes up the order of certain other places. For example, all Woo sites has the /shop/?orderby=date function, which we use to show the new arrivals in our store. But after adding this code, the new arrival products via the above link I pointed out is all a mess. It’s not showing the latest product which was added to the store anymore.
Would appreciate if your feedback in this matter ๐
Thanks again!
Hey Randika, thanks for your comment! You’ll need to either run this snippet just on the pages you want, or otherwise customize the coding to make it work with the other sorting options ๐
Hello,
Is this possible to make the code work on single product page in Related products section?
I mean – now my Related products section show often out of stock products and I would like to get rid of them or shift them to the end of the list (if there are only a few related products).
Regards,
Domin
Hello Domin, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
Kinda works, but then you lose the alphanumeric sorting, so the “in stock” items are all at top, but no longer listed alphabetically. This outcome is on current WP & Woo as of November 2018.
Hey Matt, thanks for your comment! I guess you can add an “orderby” array element and make that happen – let me know ๐
Hi.
How can I show products by two model:
first: Stock Ascending
second: publish date Descending
Thanks
Hello Ushina, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R