【发布时间】:2016-05-27 22:48:03
【问题描述】:
我希望 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;
}
任何帮助将不胜感激!
【问题讨论】:
-
为什么你在
$terms循环,但在第一个循环之后立即break? -
哎呀。那是我测试时留下的。我删除了它。 category_and 不起作用,post_in 也不起作用。我肯定在这里错过了一个概念。
标签: php wordpress woocommerce