我知道我不是第一个,我可能不会是最后一个尝试使这个旅程工作的人。我看过那里的一切。
大多数答案都是WC 2.1之前的。
很多回复都与tax_query一起使用。每隔一段时间,有人会尝试触摸查询。我试过这两个,这两个选项都不适合我。
如何调整WooCommerce中显示的相关产品以通过自定义属性包含关系?
目标:通过Cat AND(品牌或艺术家或制造商)的关系
通过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');
有人可以如此慷慨地告诉我哪里出错了吗?
非常感谢你!
答案 0 :(得分:0)
我尝试了“通过woocommerce_related_products_args进行测试”,看来您应该将'slug'更改为'slugs'以及'category'到'product_cat':)
<script>
var myWindow = window.open("", "", "width=1000,height=1000");
setTimeout("",5000);
myWindow.resizeTo(700,700);
setTimeout("",5000);
myWindow.resizeTo(500,500);
setTimeout("",5000);
</script>