[英]Wordpress / WooCommerce - Related Products by Attribute
我知道我不是第一个,而且可能不会是最后一个尝试使此旅程成功的人。 我已经查看了所有内容。
大多数答案是WC 2.1之前的版本。
很多答复都与tax_query一起使用。 有时,有人会尝试触摸查询。 我都尝试过,这两个选项都不适合我。
如何调整WooCommerce中显示的“相关产品”以通过“自定义属性”包含关系?
目标 :通过猫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');
有人可以这么客气地告诉我我要去哪里了吗?
提前非常感谢您!
我尝试了“通过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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.