• Resolved ParsMizban

    (@parsmizban)


    Hi,

    What should I do to display in stock products (with or without quantity values) ?
    I want to display them first before out of stock products in front-end

    Thank you very much

Viewing 3 replies - 1 through 3 (of 3 total)
  • @parsmizban as far as I know this isn’t a sort option. You’ll likely need to customize your template with two loops. The first loop displays only instock products and the second displays only out of stock products.

    https://docs.woocommerce.com/document/template-structure/

    Thread Starter ParsMizban

    (@parsmizban)

    Hi,

    Why this way?
    This is a very basic settings, Why we can’t sort by availability?
    I have an eshop with 30,000 products, There are many products which I want to display them in bottom because of out of stock, But I don’t want to miss them for SEO matters

    Thread Starter ParsMizban

    (@parsmizban)

    I used this code:

    
    <?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;
    
    ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Display in stock products in top of out of stocks’ is closed to new replies.