【问题标题】:Woocommerce display Reviews and Ratings by tagsWoocommerce 按标签显示评论和评分
【发布时间】:2021-12-04 10:28:03
【问题描述】:

我正在构建一个电子商务网站,并且在用户完成订单流程后,我已经安装了以下插件(Commerce 的客户评论 - https://wordpress.org/plugins/customer-reviews-woocommerce/)用于订单的评论和评级。

但是,我们处理的产品的性质(如面料、连衣裙、纱丽等)将缺货,并且无法再次采购相同的产品。因此,我想使用订单拥有的产品的“标签”显示旧订单的评论和评级(因此,我想在订单行项目上进行评论)。此外,新产品页面应该使用它自己的标签从具有相同标签的旧订单中获取评论和评级。

任何指导都会对此事有所帮助!

【问题讨论】:

    标签: wordpress woocommerce tags review rating-system


    【解决方案1】:

    要解决这个问题,首先要做的是将与给定产品关联的所有标签放入一个数组中。然后,WP_Comments_Query 需要用第一步生成的产品 id 数组进行查询。

    这是一个使用上述方法的sn-p。

    function get_reviews_by_tags(){
    
        global $product;
        $productid = $product->get_id();
        
        // get all product_tags of the current product in an array
        $current_tags = get_the_terms( $productid, 'product_tag' );
        
        //only start if we have some tags
        if ( $current_tags && ! is_wp_error( $current_tags ) ) {
            
            //get all related product ids mapped by tags array we created earlier
            $relatedproductids_by_tags = get_posts( array(
                      'post_type' => 'product',
                      'numberposts' => -1,
                      'post_status' => 'publish',
                      'fields' => 'ids',
                      'tax_query' => array(
                          array(
                              'taxonomy' => 'product_tag',
                              'field' => 'term_id',
                              'terms'     => $current_tags,
                              'operator' => 'IN'
                              )
                           ),
                       ));
           
    
            // create a wp comment query object as wc uses comments for reviews  
            $reviews_args = array(
                        'post__in' => $relatedproductids_by_tags
                        );
            $reviews_query = new WP_Comment_Query;
            $reviews = $reviews_query->query( $reviews_args );
        
            if ( !empty( $reviews ) ) {
                foreach ( $reviews as $review ) {
                    echo '<p>' . $review->comment_content . '</p>';
                }
            } else {
                echo 'No reviews found.';
            }
    }
    add_action( 'woocommerce_after_single_product_summary', 'get_reviews_by_tags', 10, 2 ); 
        
    }
    

    上面的代码不考虑您在问题中提到的插件所做的任何修改。另外,请注意,此代码用于获取和显示您的问题中提到的评论。这不是为了创建评论。

    【讨论】:

    • 非常感谢 Naveen 的回答!一旦解决方案对我有用,我将尝试并将其标记为已接受:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 2016-02-22
    • 1970-01-01
    • 1970-01-01
    • 2014-03-29
    相关资源
    最近更新 更多