WordPress的/ WooCommerce - 相关产品按属性
问题描述:
我知道我不是第一个,我可能不会是最后尝试使这个旅程工作。我已经看过那里的一切。WordPress的/ WooCommerce - 相关产品按属性
大多数答案都是WC 2.1前的版本。
许多答复与tax_query一起使用。每隔一段时间,有人试图触摸查询。我试过两种,两种都不适合我。
如何调整WooCommerce中显示的相关产品以通过自定义属性包含关系?
目标:通过猫的关系,
测试通过woocommerce_product_related_posts_query
(品牌或艺术家或制造商):
function product_related_posts_relate_by_attributes ($query){
global $product;
//*
$brands_array = wc_get_product_terms($product->id, 'pa_brand', array('fields' => 'ids'));
$artists_array = wc_get_product_terms($product->id, 'pa_artist', array('fields' => 'ids'));
$manufacturers_array = wc_get_product_terms($product->id, 'pa_manufacturer', array('fields' => 'ids'));
$query['where'] .= ' AND (';
$query['where'] .= " (tt.taxonomy = 'pa_brand' AND t.term_id IN (" . implode(',', $brands_array) . ")) ";
$query['where'] .= ' OR ';
$query['where'] .= " (tt.taxonomy = 'pa_artist' AND t.term_id IN (" . implode(',', $artists_array) . ")) ";
$query['where'] .= ' OR ';
$query['where'] .= " (tt.taxonomy = 'pa_manufacturer' AND t.term_id IN (" . implode(',', $manufacturers_array) . ")) ";
$query['where'] .= ')';//*/
return $query;
}
add_filter('woocommerce_product_related_posts_query', 'product_related_posts_relate_by_attributes');
测试通过woocommerce_related_products_args
:
function custom_related_product_args ($args){
global $product;
$cats = wc_get_product_terms($product->id, 'product_cat', array('fields' => 'slug'));
$brands = wc_get_product_terms($product->id, 'pa_brand', array('fields' => 'slug'));
$artists = wc_get_product_terms($product->id, 'pa_artist', array('fields' => 'slug'));
$manufacturers = wc_get_product_terms($product->id, 'pa_manufacturer', array('fields' => 'slug'));
unset($args['post__in']);
$args['tax_query'] = array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $cats,
),
array(
'relation' => 'OR',
array(
'taxonomy' => 'pa_brand',
'field' => 'slug',
'terms' => $brands,
),
array(
'taxonomy' => 'pa_artist',
'field' => 'slug',
'terms' => $artists,
),
array(
'taxonomy' => 'pa_manufacturer',
'field' => 'slug',
'terms' => $manufacturers,
)
)
);
return $args;
}
add_filter('woocommerce_related_products_args', 'custom_related_product_args');
可能有人请如此亲切地告诉我我要去哪里错了?
非常感谢您提前!
答
我尝试了“通过woocommerce_related_products_args测试”,看来你应该只改变“塞”到“鼻涕虫”也“类别”到“product_cat” :)
global $product;
$cats = wc_get_product_terms($product->id, 'product_cat', array('fields' => 'slugs'));
$brands = wc_get_product_terms($product->id, 'pa_brand', array('fields' => 'slugs'));
$artists = wc_get_product_terms($product->id, 'pa_artist', array('fields' => 'slugs'));
$manufacturers = wc_get_product_terms($product->id, 'pa_manufacturer', array('fields' => 'slugs'));
unset($args['post__in']);
$args['tax_query'] = array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $cats,
),
array(
'relation' => 'OR',
array(
'taxonomy' => 'pa_brand',
'field' => 'slug',
'terms' => $brands,
),
array(
'taxonomy' => 'pa_artist',
'field' => 'slug',
'terms' => $artists,
),
array(
'taxonomy' => 'pa_manufacturer',
'field' => 'slug',
'terms' => $manufacturers,
)
)
);
return $args;