显示相同产品类别和颜色内的相关产品

时间:2021-03-16 16:03:51

标签: php wordpress woocommerce

我尝试了 Display Related products for a specific product attribute value in single product pages 代码段,它在我的产品详细信息下返回以下部分:

Products having same color

然而,正如你所看到的,我还在鞋子之间得到了一个带有这种颜色的配饰(最后一件是一个包),所以我想要的是产品:

  • 具有相同的颜色
  • 具有相同的产品类别

如何修改此代码以达到此目的?

我的尝试

我可能需要在某处添加它以获取产品的当前类别:

$category = wp_get_post_terms( $post->ID, 'product_cat' );

现在,我还必须修改查询以将产品类别考虑在内。

我通过以下嵌套查询得到了我想要的结果:

SELECT DISTINCT tr.object_id

FROM {$wpdb->prefix}term_relationships as tr
JOIN {$wpdb->prefix}term_taxonomy as tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
JOIN {$wpdb->prefix}terms as t ON tt.term_id = t.term_id

WHERE tt.taxonomy LIKE 'pa_kleur' AND t.term_id = '286'
AND tr.object_id IN (
    SELECT DISTINCT tr.object_id
    FROM {$wpdb->prefix}term_relationships as tr
    JOIN {$wpdb->prefix}term_taxonomy as tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
    JOIN {$wpdb->prefix}terms as t ON tt.term_id = t.term_id
    WHERE tt.taxonomy = 'product_cat' AND t.term_id = '305'
)

现在,我仍然需要动态填充颜色 ID 和类别 ID。

1 个答案:

答案 0 :(得分:0)

在我的解决方案下面,你可以在functions.php中插入:

/* OTHER PRODUCTS OF THE SAME PRODUCT CATOGORY IN THIS COLOR */
add_action( 'woocommerce_after_single_product_summary', 'custom_output_product_collection', 12 );

function custom_output_product_collection(){

## --- YOUR SETTINGS --- ##

$attribute = "kleur"; // <== HERE define your attribute name
$limit     = "4";     // <== Number of products to be displayed
$cols      = "4";     // <== Number of columns
$orderby   = "rand";  // <== Order by argument (random order here)

## --- THE CODE --- ##

global $post, $wpdb;

// Formatting the attribute
$attribute = sanitize_title( $attribute );
$taxonomy  = 'pa_' . $attribute;

// Get the WP_Term object for the current product and the defined product attribute
$terms = wp_get_post_terms( $post->ID, $taxonomy );
$term = reset($terms);

// Set the product category
$categories = wp_get_post_terms( $post->ID, 'product_cat' );
$category = reset($categories);

// Get all product IDs that have the same product attribute value (except current product ID) & same product category
$product_ids = $wpdb->get_col
( "        
SELECT DISTINCT tr.object_id
FROM {$wpdb->prefix}term_relationships as tr
JOIN {$wpdb->prefix}term_taxonomy as tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
JOIN {$wpdb->prefix}terms as t ON tt.term_id = t.term_id
WHERE tt.taxonomy LIKE '$taxonomy' AND t.term_id = '{$term->term_id}' AND tr.object_id != '{$post->ID}
AND tr.object_id IN (
    SELECT DISTINCT tr.object_id
    FROM {$wpdb->prefix}term_relationships as tr
    JOIN {$wpdb->prefix}term_taxonomy as tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
    JOIN {$wpdb->prefix}terms as t ON tt.term_id = t.term_id
    WHERE tt.taxonomy = 'product_cat' AND t.term_id = '{$category->term_id}'
)
" );

// Convert array values to a coma separated string
$ids = implode( ',', $product_ids );

## --- THE OUTPUT --- ##

echo '<section class="'.$attribute.' '.$attribute.'-'.$term->slug.' related products">
    <h2>'.__( "Andere producten in dit kleur", "woocommerce" ).': '.$term->name.'</h2>';

echo do_shortcode("[products ids='$ids' columns='$cols' limit='$limit' orderby='$orderby']");

echo '</section>';
}

在我的网站上测试并有效: Only beige products of same product category shown