Search code examples
phphtmlwordpresswoocommerce

Display out of stock products last (even after sort)


I have a Woocommerce shop and I wanted to know how I can keep all out of stock products last in my listing.

I used the below code to keep out of stock items at the end which works fine - however when a user does a sort of price low-to-high, then the out of stock products no longer remain at the end of the listing.

Do I have to add anything to the code to always keep the product(s) last no matter what the user sorts by?

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

Solution

  • try this code (put in your theme functions.php)

    /**
    * Sorting out of stock WooCommerce products - Order product collections by stock status, in-stock 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;
    /**
    * END - Order product collections by stock status, instock products first.
    */