1

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.

1 Answer 1

2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.