【问题标题】:I want to get woocommerce reviews by product id and display it in a template我想按产品 ID 获取 woocommerce 评论并将其显示在模板中
【发布时间】:2013-10-15 02:31:45
【问题描述】:

我想按产品 ID 或其他任何内容获取 woocommerce 产品评论,并希望将其显示在我创建的模板中。

【问题讨论】:

  • 我有想法并使用 $wp_query 并在该模板中加载 single-product-reviews.php 完成它

标签: wordpress woocommerce review


【解决方案1】:

Woocommerce 使用常规 wordpress cmets 进行了评论。

简单的方法是获取 'post_type' => 'product' 的 cmets,这将获取原始评论数据。为了显示正确的 woocommerce 评论,您需要应用一个回调函数,即 'callback' => 'woocommerce_cmets'。

整件事是:

<?php
    $args = array ('post_type' => 'product');
    $comments = get_comments( $args );
    wp_list_comments( array( 'callback' => 'woocommerce_comments' ), $comments);
?>

如果您想通过产品 ID 获取评论,那么您需要更改 $args:

$args = array ('post_id' => 123); 

要对其进行更多自定义,请查看参考:

http://codex.wordpress.org/Function_Reference/wp_list_comments http://codex.wordpress.org/get_comments

'callback' => 'woocommerce_cmets' 函数使用位于 plugins/woocomerce/templates/single-product/review.php 中的模板

【讨论】:

  • 简单而且效果很好。我怎样才能同时显示评论表单?
【解决方案2】:

要显示 cmets(评论),只需使用:

echo comments_template();

在产品页面上。

更多详情请访问here

【讨论】:

    【解决方案3】:
    <div id="reviews">
    <div id="comments">
        <h2><?php
            if ( get_option( 'woocommerce_enable_review_rating' ) === 'yes' && ( $count = $product->get_rating_count() ) )
                printf( _n( '%s review for %s', '%s reviews for %s', $count, 'woocommerce' ), $count, get_the_title() );
            else
                _e( 'Reviews', 'woocommerce' );
        ?></h2>
    
        <?php if ( have_comments() ) : ?>
    
            <ol class="commentlist">
                <?php wp_list_comments( apply_filters( 'woocommerce_product_review_list_args', array( 'callback' => 'woocommerce_comments' ) ) ); ?>
            </ol>
    
            <?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) :
                echo '<nav class="woocommerce-pagination">';
                paginate_comments_links( apply_filters( 'woocommerce_comment_pagination_args', array(
                    'prev_text' => '&larr;',
                    'next_text' => '&rarr;',
                    'type'      => 'list',
                ) ) );
                echo '</nav>';
            endif; ?>
    
        <?php else : ?>
    
            <p class="woocommerce-noreviews"><?php _e( 'There are no reviews yet.', 'woocommerce' ); ?></p>
    
        <?php endif; ?>
    </div>
    
    <?php if ( get_option( 'woocommerce_review_rating_verification_required' ) === 'no' || wc_customer_bought_product( '', get_current_user_id(), $product->id ) ) : ?>
    
        <div id="review_form_wrapper">
            <div id="review_form">
                <?php
                    $commenter = wp_get_current_commenter();
    
                    $comment_form = array(
                        'title_reply'          => have_comments() ? __( 'Add a review', 'woocommerce' ) : __( 'Be the first to review', 'woocommerce' ) . ' &ldquo;' . get_the_title() . '&rdquo;',
                        'title_reply_to'       => __( 'Leave a Reply to %s', 'woocommerce' ),
                        'comment_notes_before' => '',
                        'comment_notes_after'  => '',
                        'fields'               => array(
                            'author' => '<p class="comment-form-author">' . '<label for="author">' . __( 'Name', 'woocommerce' ) . ' <span class="required">*</span></label> ' .
                                        '<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30" aria-required="true" /></p>',
                            'email'  => '<p class="comment-form-email"><label for="email">' . __( 'Email', 'woocommerce' ) . ' <span class="required">*</span></label> ' .
                                        '<input id="email" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30" aria-required="true" /></p>',
                        ),
                        'label_submit'  => __( 'Submit', 'woocommerce' ),
                        'logged_in_as'  => '',
                        'comment_field' => ''
                    );
    
                    if ( get_option( 'woocommerce_enable_review_rating' ) === 'yes' ) {
                        $comment_form['comment_field'] = '<p class="comment-form-rating"><label for="rating">' . __( 'Your Rating', 'woocommerce' ) .'</label><select name="rating" id="rating">
                            <option value="">' . __( 'Rate&hellip;', 'woocommerce' ) . '</option>
                            <option value="5">' . __( 'Perfect', 'woocommerce' ) . '</option>
                            <option value="4">' . __( 'Good', 'woocommerce' ) . '</option>
                            <option value="3">' . __( 'Average', 'woocommerce' ) . '</option>
                            <option value="2">' . __( 'Not that bad', 'woocommerce' ) . '</option>
                            <option value="1">' . __( 'Very Poor', 'woocommerce' ) . '</option>
                        </select></p>';
                    }
    
                    $comment_form['comment_field'] .= '<p class="comment-form-comment"><label for="comment">' . __( 'Your Review', 'woocommerce' ) . '</label><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>';
    
                    comment_form( apply_filters( 'woocommerce_product_review_comment_form_args', $comment_form ) );
                ?>
            </div>
        </div>
    
    <?php else : ?>
    
        <p class="woocommerce-verification-required"><?php _e( 'Only logged in customers who have purchased this product may leave a review.', 'woocommerce' ); ?></p>
    
    <?php endif; ?>
    
    <div class="clear"></div>
    

    【讨论】:

      【解决方案4】:

      是的,先生,我已经尽力在默认选项卡的底部显示评论,但我很多天都做不到,最后我找到了显示特定产品评论的方法。它工作得很好。太棒了,太棒了。

      但在 single-product.php 上动态显示产品评论还有一件事 // 动态获取产品的ID,并通过ID打印(评论)。

      global $product;
      $id = $product->id;
      
      echo $id.",";
      $args = array ('post_type' => 'product', 'post_id' => $id);
      $comments = get_comments( $args );
      wp_list_comments( array( 'callback' => 'woocommerce_comments' ), $comments);
      

      只需将此代码粘贴到您的 single-product.php 文件中即可查看结果。它会如你所愿地显示。

      【讨论】:

        【解决方案5】:

        我最近也遇到了同样的问题,我想出了这个解决方案,让您可以在一个页面上输出所有产品的评论。

        //Display all product reviews
        if (!function_exists('display_all_reviews')) {
        function display_all_reviews(){
            $args = array(
               'status' => 'approve',
               'type' => 'review'
            );
        
            // The Query
            $comments_query = new WP_Comment_Query;
            $comments = $comments_query->query( $args );
        
            // Comment Loop
            if ( $comments ) {
                echo "<ol>";
                foreach ( $comments as $comment ): ?>
                <?php if ( $comment->comment_approved == '0' ) : ?>
                    <p class="meta waiting-approval-info">
                        <em><?php _e( 'Thanks, your review is awaiting approval', 'woocommerce' ); ?></em>
                    </p>
                    <?php endif;  ?>
                    <li itemprop="reviews" itemscope itemtype="http://schema.org/Review" <?php comment_class(); ?> id="li-review-<?php echo $comment->comment_ID; ?>">
                        <div id="review-<?php echo $comment->comment_ID; ?>" class="review_container">
                            <div class="review-avatar">
                                <?php echo get_avatar( $comment->comment_author_email, $size = '50' ); ?>
                            </div>
                            <div class="review-author">
                                <div class="review-author-name" itemprop="author"><?php echo $comment->comment_author; ?></div>
                                <div class='star-rating-container'>
                                    <div itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating" class="star-rating" title="<?php echo esc_attr( get_comment_meta( $comment->comment_ID, 'rating', true ) ); ?>">
                                        <span style="width:<?php echo get_comment_meta( $comment->comment_ID, 'rating', true )*22; ?>px"><span itemprop="ratingValue"><?php echo get_comment_meta( $comment->comment_ID, 'rating', true ); ?></span> <?php _e('out of 5', 'woocommerce'); ?></span>
        
                                            <?php
                                                $timestamp = strtotime( $comment->comment_date ); //Changing comment time to timestamp
                                                $date = date('F d, Y', $timestamp);
                                            ?>
                                    </div>
                                    <em class="review-date">
                                        <time itemprop="datePublished" datetime="<?php echo $comment->comment_date; ?>"><?php echo $date; ?></time>
                                    </em>
                                </div>
                            </div>
                            <div class="clear"></div>
                            <div class="review-text">
                                <div itemprop="description" class="description">
                                    <?php echo $comment->comment_content; ?>
                                </div>
                                <div class="clear"></div>
                            </div>
                        <div class="clear"></div>           
                    </div>
                </li>
        
                <?php 
                endforeach;
                echo "</ol>";
            } else {
                echo "This product hasn't been rated yet.";
            }
        }
        }
        

        将上述函数添加到您的functions.php 文件中。 在此之后,您可以在任何您喜欢的主题上使用此功能,方法是这样调用:

        <?php echo display_all_reviews(); ?>
        

        我还在这里创建了一个教程https://www.majas-lapu-izstrade.lv/get-woocommerce-customer-reviews-from-all-products-display-average-and-all-ratings-in-a-histogram-without-a-plugin/,其中还包括输出平均产品评分、所有产品的所有评分总数和所有产品评论直方图的函数。

        【讨论】:

          【解决方案6】:

          woocommerce/templates/single-product/tabs/tabs.php

          如果您在声明后对$tabs 运行print_r(),您会看到部分输出如下:

          [reviews] => Array (
              [title] => Reviews (3)
              [priority] => 30
              [callback] => comments_template
          )
          

          这意味着回调是函数:comments_template()

          由于此函数不接受$post_id 参数,因此需要在循环中调用。

          但是,该函数确实接受要使用的模板文件的参数,因此要回答 OP 关于如何使用自定义模板文件的直接问题 - 您可以像这样使用该函数:

          comments_template( string $file = '/comments.php', bool $separate_comments = false );
          

          This ^ was taken directly from the codex

          【讨论】:

            【解决方案7】:

            自从您不久前发布以来,Kishan 可能有点晚了,但也许它会对其他人有所帮助。

            我们刚刚开发了一个免费插件,可以通过带有简码的产品 ID 显示 woocommerce 评论。它还插入了产品的 Schema。

            看看吧,它是免费的:https://shopitpress.com/plugins/sip-reviews-shortcode-woocommerce/

            【讨论】:

            • 在此处添加相关文字。不鼓励仅外部链接的答案,因为将来链接会发生变化
            【解决方案8】:

            我尝试了很多方法来获得任何内置函数,但我没有。然后我写了我自己的查询。它可能会对您有所帮助:

            $comment_and_reviews = $wpdb->get_results("SELECT wpc.comment_author,wpc.comment_author_email,wpc.comment_date,wpc.comment_content,wpcm.meta_value AS rating FROM `" . $wpdb->prefix . "comments` AS wpc INNER JOIN  `" . $wpdb->prefix . "commentmeta` AS wpcm ON wpcm.comment_id = wpc.comment_id AND wpcm.meta_key = 'rating' WHERE wpc.comment_post_id = '" . $p_id . "' ");
            

            【讨论】:

              【解决方案9】:

              我花了一周的时间找到并找到了一种简单的方法。

              如果您想将评论产品带到页面,只需使用 WP_Query 并输入 echo comments_template()里面

              或者,如果您想要自定义模板审核,您可以使用 wp-content/plugins/woocommerce/templates/single-product-reviews.php 中的模板并放入 WP_Query

              以我为例,我想将产品 ID 33051 的评论带到主页

              <?php   
                  $args = array('post_type' => 'product','id' => 33051);
                  $result = new WP_Query( $args );
                  if ( $result-> have_posts() ) : ?>
                  <?php while ( $result->have_posts() ) : $result->the_post(); ?>
                  <?php comments_template(); // you can custom template by code in single-product-reviews.php
              ?>
              <?php endwhile; ?>
              <?php endif; wp_reset_postdata(); ?>
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2016-03-04
                • 1970-01-01
                • 2012-06-30
                • 1970-01-01
                • 2019-06-02
                • 1970-01-01
                • 2021-05-05
                相关资源
                最近更新 更多