I know I'm not the first and I probably won't be the last to try to make this journey work. I've looked at everything that's out there.
Most answers are pre WC 2.1.
A lot of the replies work with the tax_query. Every once in a while, someone tries to touch the query. I've tried both, neither option works for me.
How do I adjust the Related Products shown in WooCommerce to include relationships via Custom Attributes?
Goal : Relationship via Cat AND (Brand OR Artist OR Manufacturer)
Test via 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');
Test via 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');
Could someone please be so gracious as to tell me where I'm going wrong?
Thank you very much in advance!
I tried the "test via woocommerce_related_products_args" and it appears you should just change the 'slug' to 'slugs' and also 'category' to '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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.