简体   繁体   English

WP_Query Woocommerce按属性和价格变动过滤可变产品正价

[英]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'. 我有可变产品,它的属性名为:“数量”,其中有“ 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. 我想过滤并显示数量变化为'200'且价格为正常价格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. 我正在使用此代码,但它显示的是数量变化为'200'的所有产品,但未按变化的常规价格进行过滤。

            $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. 例如,我使用的是数量属性的术语200的ID(即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. 这是查询,该查询将首先为具有术语ID 75的属性Quantity过滤变量产品,然后将进一步为价格为500的变化过滤结果。

            $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. 在示例代码中,您没有在放入元查询之前关闭tax_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 以下是查询的正确版本,在启动meta_query之前关闭tax_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'
                ),
            ),
        );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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