简体   繁体   中英

WP_Query Woocommerce Filter Variable Products by Attributes and Variations Regular Price

I have variable products and it has attribute named: 'Quantity' which has variations like '100', '200', '500'. All these variations have regular prices. I want to filter and display variables products which has Quantity variation '200' and has regular price 10.

I am using this code but it's showing all products which has Quantity variation '200' but not filtering by variation's regular price.

            $query = array(
                'post_status' => 'publish',
                'post_type' => array('product', 'product_variation'),
                'posts_per_page' => 10,
                'tax_query' => array(
                    'relation' => 'AND',
                    array(
                        'taxonomy' => 'pa_quantity',
                        'field' => 'term_id',
                        'terms' => '67',
                    ),
                'meta_query' => array(
                    array(
                        'key' => '_regular_price',
                        'value' => 10,
                        'compare' => '=',
                        'type' => 'NUMERIC'
                    ),
                ),
                )
            );

For instance, I am using attribute Quantity's term 200's id which is 67.

So, I finally figured out the answer... This may help someone in future.

Here is the query that will first filter variable products for attribute Quantity which has term id 75 and then it will further filter result for variations which has price 500 set.

            $query = array(
                'post_status' => 'publish',
                'post_type' => array('product', 'product_variation'),
                'posts_per_page' => 10,
                'tax_query' => array(
                    'relation' => 'AND',
                    array(
                    'taxonomy' => 'pa_quantity',
                    'field' => 'term_id',
                    'terms' => '75',
                    ),
                ),
                'meta_key' => '_price',
                'meta_value' => 500,
            );

            $wc_query = new WP_Query($query);

In your example code you haven't close the tax_query array before putting in the meta query.

Also you can try not using meta query if you only have one meta field value you are after. Instead, define the meta key and the meta value like this:

$query = array(
            'post_status' => 'publish',
            'post_type' => array('product', 'product_variation'),
            'posts_per_page' => 10,
            'tax_query' => array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'pa_quantity',
                    'field' => 'term_id',
                    'terms' => '67',
                ),
            ),
            'meta_key' => '_regular_price',
            'meta_value' => 10,
        );

Below is a corrected version of your query closing tax_query before starting meta_query

$query = array(
            'post_status' => 'publish',
            'post_type' => array('product', 'product_variation'),
            'posts_per_page' => 10,
            'tax_query' => array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'pa_quantity',
                    'field' => 'term_id',
                    'terms' => '67',
                ),
            ),
            'meta_query' => array(
                array(
                    'key' => '_regular_price',
                    'value' => 10,
                    'compare' => '=',
                    'type' => 'NUMERIC'
                ),
            ),
        );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM