Woocommerce: display products with same short description

Question

I’m trying to display products with same short description or even the description/content as related products. I’ve tried two ways but it doesn’t seem to work.

1st method

add_filter( 'woocommerce_related_products', 'cusfu_related_products_by_same_desc', 9999, 3 ); 

function cusfu_related_products_by_same_desc( $related_posts, $product_id, $args ) {
   $product = wc_get_product( $product_id );
   $shortdesc = $product->get_short_description();
   $related_posts = get_posts( array(
      'post_type' => 'product',
      'post_status' => 'publish',
      'short_description' => $shortdesc,
      'fields' => 'ids',
      'exclude' => array( $product_id ),
   ));
   return $related_posts;
}

2nd method

add_filter( 'woocommerce_related_products', 'cusfu_related_products_by_same_desc', 9999, 3 ); 
function cusfu_related_products_by_same_desc( $related_posts, $product_id, $args ) {
   $product = wc_get_product( $product_id );
   $shortdesc = $product->get_short_description();
   $related_posts = get_posts( array(
      'post_type' => 'product',
      'post_status' => 'publish',
      'meta_key' => 'short_description'
      'meta_value' => $shortdesc,
      'fields' => 'ids',
      'exclude' => array( $product_id ),
   ));
   return $related_posts;
}

Any help is appreciated.

0
Billy 2 months 0 Answers 13 views 0

Leave an answer