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.
Go to WooCommerce -> Settings and navigate to the Products tab. Select Inventory. Check the 'Out Of Stock Visibility' option to hide out of stock items from your catalog.
Navigate to WooCommerce > Settings > Product > Inventory. There is a setting "Stock Display Format". Select the "Never Show Stock Amount" from the drop down.
If the 'Stock display format' option on WooCommerce → Settings → Products → Inventory is set to display the quantity remaining in stock, then every product will have detailed stock information.
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. We JOIN
the wp_postmeta table to the query and prepend an orderby _stock_status
clause to the existing query. This way any other orderby
clauses remain in the query as well.
add_filter('posts_clauses', 'order_by_stock_status'); function order_by_stock_status($posts_clauses) { global $wpdb; // only change query on WooCommerce loops if (is_woocommerce() && (is_shop() || is_product_category() || is_product_tag() || is_product_taxonomy())) { $posts_clauses['join'] .= " INNER JOIN $wpdb->postmeta istockstatus ON ($wpdb->posts.ID = istockstatus.post_id) "; $posts_clauses['orderby'] = " istockstatus.meta_value ASC, " . $posts_clauses['orderby']; $posts_clauses['where'] = " AND istockstatus.meta_key = '_stock_status' AND istockstatus.meta_value <> '' " . $posts_clauses['where']; } return $posts_clauses; }
You could change istockstatus.meta_value ASC
to istockstatus.meta_value DESC
if you for some reason wanted the Out Of Stock items first.
Tested on WP: 4.8; WC 3.0.8
Here is a snippet for rearranging products (in stock come first):
<?php /** * Order product collections by stock status, instock products first. */ class iWC_Orderby_Stock_Status { public function __construct() { // Check if WooCommerce is active if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) { add_filter('posts_clauses', array($this, 'order_by_stock_status'), 2000); } } public function order_by_stock_status($posts_clauses) { global $wpdb; // only change query on WooCommerce loops if (is_woocommerce() && (is_shop() || is_product_category() || is_product_tag())) { $posts_clauses['join'] .= " INNER JOIN $wpdb->postmeta istockstatus ON ($wpdb->posts.ID = istockstatus.post_id) "; $posts_clauses['orderby'] = " istockstatus.meta_value ASC, " . $posts_clauses['orderby']; $posts_clauses['where'] = " AND istockstatus.meta_key = '_stock_status' AND istockstatus.meta_value <> '' " . $posts_clauses['where']; } return $posts_clauses; } } new iWC_Orderby_Stock_Status; ?>
https://www.snip2code.com/Snippet/114858/WooCommerce-Products-Order-by-Stock-Stat
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With