Is it possible to show out of stock products at the end of a category or page in wordpress?
So the customer first see the products that are available and after that the products that are out of stock.
Is it possible to show out of stock products at the end of a category or page in wordpress?
So the customer first see the products that are available and after that the products that are out of stock.
You must be logged in to post a comment.
This is the same as Viktor & Bogdan’s answer, but without the extra Class code.
It uses the
post_clause
filter to modify the product query. WeJOIN
the wp_postmeta table to the query and prepend anorderby _stock_status
clause to the existing query. This way any otherorderby
clauses remain in the query as well.You could change
istockstatus.meta_value ASC
toistockstatus.meta_value DESC
if you for some reason wanted the Out Of Stock items first.Tested on WP: 4.8; WC 3.0.8
I tried all the solutions above, they worked but resulted in other issues on the site (probably due to theme conflict), Im sure they are all good in different situations / themes. The below code finally worked great for me (source mentioned)
source: https://www.businessbloomer.com/woocommerce-show-in-stock-products-first-shop/
Here is a snippet for rearranging products (in stock come first):
https://www.snip2code.com/Snippet/114858/WooCommerce-Products-Order-by-Stock-Stat
@dacrosby answer really should be the best answer here. I’ve had a few issues with compatibility with some plugins but in most use cases it seems to work with no issues at all.
That being said here are a few caveats
This version built on top of the same implementation should provide wider compatibility without sacrificing the desired result.
Access the global configuration options for inventory management in WooCommerce, look to the left of your WordPress admin and click on WooCommerce, then on Settings, then click on the Inventory tab.
You will find this “Out of Stock Visibility”
Out of Stock Visibility – This checkbox will allow you to determine if you want to hide out of inventory items within the WooCommerce catalog.
http://www.inmotionhosting.com/support/website/woocommerce/managing-inventory-in-woocommerce
For making them appear at the end of the category you could use pre_get_posts to order based on the stock, but then you’ll lose your other sorting.
Try this code (put in functions.php of your theme):
Taken here.
For the last versions of WooCommerce see answer below: https://stackoverflow.com/a/44597448/3925099
This is the best solution:
This code work for me:
Edited one answer above:
As an alternative, simple solution, if your product list is in a flexbox, you could do it with CSS too with the following rule:
I think this query can be better, because if a site sells its products by pre-order, it will be displayed at the end of the list.
This query is the optimized version that you can use.
Also, pre-order products will remain in place:
This code adds a left join to the postmeta table to retrieve the _stock_status meta value, and orders the results first by whether the product is out of stock, and then by the original orderby value. This filter only applies to product category queries, and won’t affect other queries.
This approach should be relatively efficient because it doesn’t require an additional query, but rather modifies the existing query with a small additional join and sort.