相关产品查询类别和属性

时间:2014-12-17 19:52:07

标签: php wordpress woocommerce

我希望woocommerce显示与类别和属性匹配的相关产品。当我查看get_related函数时,我不确定如何向查询添加pa_city必须与变量$ city匹配。我试过这个钩子,但它仍然只匹配类别,查询中没有使用城市,不明白为什么:

add_filter( 'woocommerce_product_related_posts',
           'relate_city' );
function relate_city() {
    $city = isset( $_COOKIE['newcity'] ) ? $_COOKIE['newcity'] : 'not set';
    $get_related_products_args = array(
     'orderby' => 'rand',
     'posts_per_page' => $limit,
     'post_type' => 'product',
     'fields' => 'ids',
     'meta_query' => $meta_query,
     'tax_query' => array(
       'relation' => 'AND',
       array(
         'taxonomy' => 'product_cat',
         'field' => 'id',
         'terms' => $cats_array
       ),
       array(
         'taxonomy' => 'pa_city',
         'field' => 'slug',
         'terms' => array ($city)
       )
     )
    );
    return $get_related_products_args;
}

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

试试这个

add_filter( 'woocommerce_product_related_posts', 
                'relate_city' );
    function relate_city() {
    $city = isset( $_COOKIE['newcity'] ) ? $_COOKIE['newcity'] : 'not set';
            $get_related_products_args = array(
                'orderby' => 'rand',
                'posts_per_page' => $limit,
                'post_type' => 'product',
                'fields' => 'ids',
                'meta_query' => $meta_query,
                'tax_query' => array(
                    'relation' => 'AND',
                    array(
                        'taxonomy' => 'product_cat',
                        'field' => 'id',
                        'terms' => $cats_array
                    ),
                array(
                'key' => 'pa_city',
                'value' => $city,
                 'compare' => '='
                   )
                )
            );
            return $get_related_products_args;
    }