【问题标题】:Related Products query category and attribute相关产品查询类别和属性
【发布时间】: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


【解决方案1】:

试试这个

add_filter('woocommerce_product_related_posts', 'relate_city'); 功能相关城市(){ $city = isset($_COOKIE['newcity']) ? $_COOKIE['newcity'] : '未设置'; $get_related_products_args = 数组( 'orderby' => 'rand', 'posts_per_page' => $limit, 'post_type' => '产品', '字段' => 'ids', 'meta_query' => $meta_query, 'tax_query' => 数组( '关系' => '与', 大批( '分类' => 'product_cat', '字段' => 'id', '条款' => $cats_array ), 大批( 'key' => 'pa_city', '价值' => $城市, '比较' => '=' ) ) ); 返回 $get_related_products_args; }

【讨论】:

    猜你喜欢
    • 2011-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-22
    相关资源
    最近更新 更多