G
G
Grizar2020-03-11 01:33:25
WordPress
Grizar, 2020-03-11 01:33:25

Products without a price or with a price of 0 at the end of the list?

I have a shop on wordpress + wookomers theme storefront.
How to make products without a price or with a price of 0 shown at the end of the list of products and excluded from sorting?
These codes don't work.

add_filter('posts_clauses', 'order_by_stock_status', 50, 2);
function order_by_stock_status($posts_clauses) {
    global $wpdb;
    if (!is_admin() && is_woocommerce() && (is_shop() || is_product_category() || is_product_tag() || is_product_taxonomy())) {
        $posts_clauses['orderby'] = "price_query.price > 0 DESC, " . $posts_clauses['orderby'];
    }
    return $posts_clauses;
}

and this
add_filter('posts_clauses', 'order_by_no_price', 50, 2);
function order_by_no_price($posts_clauses) {
    global $wpdb;
    if (!is_admin() && is_woocommerce() && (is_shop() || is_product_category() || is_product_tag() || is_product_taxonomy())) {
        if($posts_clauses['join']!=' LEFT JOIN wp_wc_product_meta_lookup wc_product_meta_lookup ON wp_posts.ID = wc_product_meta_lookup.product_id '){
            $posts_clauses['join'] = $posts_clauses['join'] . " LEFT JOIN wp_wc_product_meta_lookup wc_product_meta_lookup ON wp_posts.ID = wc_product_meta_lookup.product_id ";   
        }       
        $posts_clauses['orderby'] = "wc_product_meta_lookup.min_price > 0 DESC, " . $posts_clauses['orderby'];
    }   
    return $posts_clauses;
}

Three years old question with no solution How to sort products in Woocommerce so that products without a price or with a price of 0 are shown at the end of the list?
Woocommerce product sorting not working: to show products with no price or with price 0 at the end of the list?
==============
It is STRANGE that the site moderator changed the status of the question from difficult to easy, there is no solution for three years and the question is easy\. so write the solution if so.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question