Quantcast
Channel: WordPress.org Forums » [WooCommerce] Support
Viewing all articles
Browse latest Browse all 104029

IgniteWoo.com Team on "[Plugin: WooCommerce - excelling eCommerce] Related Products Filter"

$
0
0

Actually editing the WooCommerce core code is not the best way to handle things since when you update the plugin you lose changes. So do something like this instead, in your theme functions file probably ( or make it into a plugin, which is simple enough to do )

add_filter( 'woocommerce_product_related_posts', 'my_related_products', 20, 1 ) ;

function my_related_products( $related_products = array() ) {
  global $post, $woocommerce;

  $limit = 5;

  $terms = wp_get_post_terms( $post->ID, 'product_tag' );

  if ( !$terms || is_wp_error( $terms ) )
    return $related_products;

  if ( empty( $terms ) )
    return array();

  $tags_array = array(); 

  foreach ( $terms as $term )
    $tags_array[] = $term->term_id;

  $meta_query = array();

  $meta_query[] = $woocommerce->query->visibility_meta_query();

  $meta_query[] = $woocommerce->query->stock_status_meta_query();

  $related_products = get_posts( array(
	'orderby' 	=> 'rand',
	'posts_per_page'=> $limit,
	'post_type' 	=> 'product',
	'fields' 	=> 'ids',
	'meta_query' 	=> $meta_query,
	'tax_query' 	=> array(
		array(
			'taxonomy' 	=> 'product_tag',
			'field' 	=> 'id',
			'terms' 	=> $tags_array
		)
	)
  ));

  return $related_products;

}

Viewing all articles
Browse latest Browse all 104029

Trending Articles