2014-12-13 5 views
3

태그를 기반으로 관련 제품을 표시하고 싶습니다. 몇 가지 방법을 시도했지만 그들 중 누구도 일하지 않았습니다. 사람이 알고 (하지 범주) 태그를 기준으로 표시 할 관련 제품을 강제 할 수있는 방법이 있습니까 여기WooCommerce - 관련 제품을 태그로 표시

이다 나는 (내가 functions.php 내부 코드의 조각을 추가) 그렇게하는 데 사용되는 코드 :

//New "Related Products" function for WooCommerce 
function get_related_custom($id, $limit = 5) { 
    global $woocommerce; 

    // Related products are found from category and tag 
    $tags_array = array(0); 
    $cats_array = array(0); 

    // Get tags 
    $terms = wp_get_post_terms($id, 'product_tag'); 
    foreach ($terms as $term) $tags_array[] = $term->term_id; 

    // Get categories (removed by NerdyMind) 
/* 
    $terms = wp_get_post_terms($id, 'product_cat'); 
    foreach ($terms as $term) $cats_array[] = $term->term_id; 
*/ 

    // Don't bother if none are 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(); 

    // Get the 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'); 

답변

4

이 당신을 위해 무엇을해야하지만 single-product.php 또는 /woocommerce/single-product/related.php

<?php 
    global $post; 

    $cats = wp_get_post_terms($post->ID, "product_cat"); 
    foreach ($cats as $cat) { 
     $cats_array[] .= $cat->term_id; 
    } 

    $tags = wp_get_post_terms($post->ID, "product_tag"); 
    foreach ($tags as $tag) { 
     $tags_array[] .= $tag->term_id; 
    } 

    $related_posts = new WP_Query(
     array(
      'orderby' => 'rand', 
      'posts_per_page' => 5, 
      'post_type' => 'product', 
      'post__not_in' => array($post->ID), 
      'tax_query' => array(
       /* 
        'relation' => 'OR', 
        array(
          'taxonomy' => 'product_cat', 
          'field' => 'id', 
          'terms' => $cats_array 
        ), 
       */ 
        array(
          'taxonomy' => 'product_tag', 
          'field' => 'id', 
          'terms' => $tags_array 
        ) 
      ) 
     ) 
    ); 
?> 
이를 추가해야
관련 문제