php - WooCommerce - related products by tags and categories -


i want display 8 "related products" in every product page of site based on tags. if there less 8 results fill gaps products in same categories.

here code i'm using showing tag-related products (functions.php):

//new "related products" function woocommerce function get_related_custom( $id, $limit = 5 ) { global $woocommerce;  // related products found category , tag $tags_array = array(0); $cats_array = array(0);  // tags $terms = wp_get_post_terms($id, 'product_tag'); foreach ( $terms $term ) $tags_array[] = $term->term_id;  // categories (removed / commented) /* $terms = wp_get_post_terms($id, 'product_cat'); foreach ( $terms $term ) $cats_array[] = $term->term_id;  */  // don't bother if none set if ( sizeof($cats_array)==1 && sizeof($tags_array)==1 ) return array();  // meta query $meta_query = array(); $meta_query[] = $woocommerce->query->visibility_meta_query(); $meta_query[] = $woocommerce->query->stock_status_meta_query();  // posts $related_posts = get_posts( apply_filters('woocommerce_product_related_posts', array(     'orderby'        => 'rand',     'posts_per_page' => $limit,     'post_type'      => 'product',     'fields'         => 'ids',     'meta_query'     => $meta_query,     'tax_query'      => array(         'relation'      => 'or',         array(             'taxonomy'     => 'product_cat',             'field'        => 'id',             'terms'        => $cats_array         ),         array(             'taxonomy'     => 'product_tag',             'field'        => 'id',             'terms'        => $tags_array         )     ) ) ) ); $related_posts = array_diff( $related_posts, array( $id )); return $related_posts; } add_action('init','get_related_custom'); 

open functions.php file in wp-content/themes/your-theme-name/ , add code @ end of file:

/**  * not filter related products tag  */ add_filter( 'woocommerce_product_related_posts_relate_by_tag', '__return_false' );  /**  * not filter related products category  */ add_filter( 'woocommerce_product_related_posts_relate_by_category', '__return_false' ); 

Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -