I've found this great solution to display related products based on a product attribute, but additionally we would like to do the same with our upsells .
What I've tried so far doesn't work yet because I believe we have to add some additonal information as the related_products function is not the same as upsells.
add_filter( 'woocommerce_upsell_products', 'upsell_products_by_attribute', 10, 3 );
function upsell_products_by_attribute( $upsells, $product_id, $args ) {
$taxonomy = 'pa_attribute';
$term_slugs = wp_get_post_terms( $product_id, $taxonomy, ['fields' => 'slugs'] );
if ( empty($term_slugs) )
return $upsells;
$posts_ids = get_posts( array(
'post_type' => 'product',
'ignore_sticky_posts' => 1,
'posts_per_page' => 6,
'post__not_in' => array( $product_id ),
'tax_query' => array( array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $term_slugs,
) ),
'fields' => 'ids',
'orderby' => 'rand',
) );
return count($posts_ids) > 0 ? $posts_ids : $upsells;
}
So I wonder if there is any possibility to do the same with the upsell section.