You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于标签实现Woocommerce旧订单评论评级在新产品页调用的咨询

实现方案指导

一、实现订单项维度的评论存储与展示

1. 扩展评论元数据字段

利用Customer reviews for Commerce插件提供的crwc_after_save_review钩子,在用户提交评论时额外存储2个自定义字段:

  • _review_order_item_id:绑定当前评论对应的订单商品条目ID,实现订单项维度的关联
  • _review_rel_tags:自动同步当前订单项对应商品的所有产品标签ID集合,方便后续标签匹配查询

示例代码:

add_action('crwc_after_save_review', function($review_id, $review_data, $order) {
    // 遍历订单条目匹配当前评论关联的商品
    foreach($order->get_items() as $item_id => $item) {
        if($item->get_product_id() == $review_data['product_id']) {
            // 存储订单项ID
            update_comment_meta($review_id, '_review_order_item_id', $item_id);
            // 存储关联商品标签ID集合
            $product = $item->get_product();
            $tag_ids = wp_get_post_terms($product->get_id(), 'product_tag', ['fields' => 'ids']);
            update_comment_meta($review_id, '_review_rel_tags', $tag_ids);
            break;
        }
    }
}, 10, 3);

2. 旧订单页订单项评论展示

在订单详情页的每个商品条目下方,拉取对应绑定的评论展示,示例代码:

add_action('woocommerce_order_item_meta_end', function($item_id, $item, $order) {
    // 查询当前订单项绑定的已审核评论
    $reviews = get_comments([
        'meta_key' => '_review_order_item_id',
        'meta_value' => $item_id,
        'post_type' => 'product',
        'status' => 'approve'
    ]);
    if(!empty($reviews)) {
        echo '<div class="order-item-reviews">';
        foreach($reviews as $review) {
            // 输出评级、评论内容等信息
            $rating = get_comment_meta($review->comment_ID, 'rating', true);
            echo '<p>评级:'.str_repeat('⭐', $rating).'<br>评论内容:'.$review->comment_content.'</p>';
        }
        echo '</div>';
    }
}, 10, 3);

二、实现新品详情页按标签匹配旧评论展示

获取当前新品的所有产品标签,查询所有命中对应标签的历史评论,聚合后在详情页展示,示例代码:

add_action('woocommerce_after_single_product_summary', function() {
    global $product;
    // 获取当前产品的标签ID
    $current_tag_ids = wp_get_post_terms($product->get_id(), 'product_tag', ['fields' => 'ids']);
    if(empty($current_tag_ids)) return;

    // 查询匹配标签的已审核评论
    $matched_reviews = get_comments([
        'status' => 'approve',
        'post_type' => 'product',
        'meta_query' => [
            [
                'key' => '_review_rel_tags',
                'value' => $current_tag_ids,
                'compare' => 'IN'
            ]
        ],
        'number' => 20 // 可自定义展示数量
    ]);

    if(!empty($matched_reviews)) {
        echo '<div class="related-history-reviews">';
        echo '<h3>同类商品历史评价</h3>';
        foreach($matched_reviews as $review) {
            $rating = get_comment_meta($review->comment_ID, 'rating', true);
            $review_product = wc_get_product($review->comment_post_ID);
            echo '<div class="review-item">';
            echo '<p>商品:'.$review_product->get_name().' | 评级:'.str_repeat('⭐', $rating).'</p>';
            echo '<p>评论:'.$review->comment_content.'</p>';
            echo '</div>';
        }
        echo '</div>';
    }
}, 20);

注意事项

  • 如需提升查询性能,可定期将评论标签关联关系同步到自定义索引表,降低元数据查询的性能损耗
  • 可根据业务需求调整评论匹配规则,比如要求同时命中多个标签才展示、按标签匹配度排序展示评论
  • 代码片段可直接添加到主题的functions.php文件,或者自定义功能插件中,修改前请做好站点备份

内容的提问来源于stack exchange,提问作者Raj Pawan Gumdal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 15:36:03