Related Products Override WooCommerce

0.0/5
<?php
add_filter( 'woocommerce_related_products', 'bbloomer_related_products_by_same_title', 9999, 3 ); 
 
function bbloomer_related_products_by_same_title( $related_posts, $product_id, $args ) {
    global $wp_query;
	$product = wc_get_product( $product_id );
    $title = $product->get_name();
	
	$terms = get_the_terms( $post->ID, 'product_cat' )[0]->name;
	foreach ($terms as $term) {
    $product_cat_id = $term->term_id;
    	break;
	}

   //$categories = get_the_category();
   $related_posts = get_posts( array(
      'post_type' => 'product',
      'post_status' => 'publish',
      'fields' => 'ids',
      'posts_per_page' => 4,
      'product_cat' => $terms,
      'exclude' => array( $product_id ),
   ));
   return $related_posts;
}

Comments and reviews

{{ reviewsTotal }} Review
{{ reviewsTotal }} Reviews
{{ options.labels.newReviewButton }}
{{ userData.canReview.message }}

Description

Author

Tags