Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi @benjaminouiart,

    Try using this:

    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;
    }

    This snippet comes from here: https://stackoverflow.com/questions/25113581/show-out-of-stock-products-at-the-end-in-woocommerce/44597448#44597448

    I tested this locally and it works on my test site.

    Let us know how that goes!

    Thank you,
    Joey

    We haven’t heard back from you in a while, so I’m going to mark this as resolved. Feel free to start a new thread if you have any further questions!

    The code should work just fine, just try changing the priority on the filter, for example like this:
    add_filter( 'posts_clauses', 'order_by_stock_status', 999 );

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Sort the products in “out of stock” at the end of the page’ is closed to new replies.