Hello,
I understand that many people need to put out of stock item last even after sorting the page, custom code is one alternative to do this.
Another alternative is using plugins, you can consider using this plugin for this job:
https://wordpress.org/plugins/woo-out-of-stock-last/
I hope this helps 🙂
Is there something I could add to the snippet that you know about? To not install a plugin
Hi @refrivan
You could check this forum thread to which the code came from since the comments provided several alternative solutions.
https://stackoverflow.com/questions/25113581/show-out-of-stock-products-at-the-end-in-woocommerce
Meanwhile, these forums are meant for general support with the core functionality of WooCommerce itself. What you want to achieve would require customization to do it. Since custom coding is outside our scope of support, I am leaving this thread open for a bit to see if anyone can chime in to help you out.
For questions related to development and custom coding, your best bet is to ask on any of these channels for support. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, too.
– WooCommerce Developer Resources Portal
– WooCommerce Advanced Facebook group
– WooCommerce Community Forum
– WooCommerce Developer Slack Channel.
– Hire a WooCommerce Expert
function _nok_order_by_stock_status( $posts_clauses, $query ) {
// only change query on WooCommerce loops
if ( $query->is_main_query() && ( is_product_category() || is_product_tag() || is_product_taxonomy() || is_shop() ) ) {
global $wpdb;
$posts_clauses['join'] .=
" LEFT JOIN (
SELECT post_id, meta_id, meta_value FROM $wpdb->postmeta
WHERE meta_key = '_stock_status' AND meta_value <> ''
) istockstatus ON ($wpdb->posts.ID = istockstatus.post_id) ";
$posts_clauses['orderby'] =
" CASE istockstatus.meta_value WHEN
'outofstock' THEN 1
ELSE 0
END ASC, " . $posts_clauses['orderby'];
}
return $posts_clauses;
}
add_filter( 'posts_clauses', '_nok_order_by_stock_status', 2000, 2 );
This one worked for me, thanks
Hi @refrivan
I’m glad we were able to find a solution to your inquiry here and thanks for sharing it with the community too! 🙂
If you have a few minutes, we’d love it if you could leave us a review:
https://wordpress.org/support/plugin/woocommerce/reviews/
Thanks!