Hi @shashikant91! How’s it going today?
Yes, you can do it! There’s a great discussion about it in the links below, and you can also find some snippets to use.
Here and Here!
Also, make sure that the option to hide the out-of-stock products is unchecked from your WooCommerce settings.
You can find it via: WordPress Dashboard > WooCommerce > Settings > products > Invetory.
Image link if needed: https://snipboard.io/lEGb64.jpg
I hope it helps and let us know how it goes!
My very best,
Hi @shashikant91,
I found a snippet of code that works well for me. You can add this in your child theme’s functions.php file :
if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
add_filter('posts_clauses', 'order_by_stock_status', 2000);
}
function order_by_stock_status($posts_clauses) {
global $wpdb;
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;
}
I hope this is helpful!
Hi @kaavyaiyer
Thank you so much for the code snippet. Appreciate your help!
I hope this works along with the sorting option – sort by latest products. Currently we are having this as our default option – sorting by latest products.
Thanks
Shashikant
Hi @shashikant91
I hope the snippet has worked out for you. I’m going to mark this thread as resolved now. If you have a few minutes, we’d love if you could leave us a review: https://wordpress.org/support/plugin/woocommerce/reviews/