Home Forums WoodMart support forum Order products shop listing by name ASC and Stock status

Order products shop listing by name ASC and Stock status

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #559563

    Hi Team, I need to order by default the product archive by Name (asc) and also get the out of stock product at the end of the listing, it is that possible?

    thanks.

    #559750

    Hello,

    Navigate to Appearance > Customize > WooCommerce > Default product sorting.

    To show an out of stock product at the end of the archive pages, Try to add the following code in the function.php of the child theme.

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

    Best Regards.

Tagged: 

Viewing 2 posts - 1 through 2 (of 2 total)