programing

제품 ID별로 woocommerce 리뷰를 받아 템플릿으로 표시하고 싶다.

iphone6s 2023. 3. 3. 17:07
반응형

제품 ID별로 woocommerce 리뷰를 받아 템플릿으로 표시하고 싶다.

제품 ID 등으로 woocommerce 제품 리뷰를 가져와 제가 만든 템플릿으로 표시하고 싶습니다.

Woocommerce는 일반적인 워드프레스 코멘트로 리뷰를 작성했다.

간단한 방법은 'post_type' = > 'product'의 코멘트를 취득하여 raw comment 데이터를 취득하는 것입니다.적절한 woocommerce 리뷰를 표시하려면 콜백 함수 witch를 적용해야 합니다.는 'callback' = > 'woocommerce_displays'입니다.

중요한 것은 다음과 같습니다.

<?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_filename' 함수는 plugins/woocomerce/filename/single-product/review에 있는 템플릿을 사용합니다.php

코멘트(리뷰)를 표시하려면 , 다음의 조작을 실시합니다.

echo comments_template();

를 참조하십시오.

상세한 것에 대하여는, 여기를 봐 주세요.

<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>

네, 기본 탭 하단에 리뷰를 표시하기 위해 최선을 다했지만 며칠 동안 할 수 없고 마지막으로 특정 제품의 리뷰를 표시하는 방법을 찾았습니다.잘 되고 있어요.멋지다, 멋지다.

단, 제품의 리뷰를 단일 제품에 동적으로 표시해야 합니다.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);

이 코드를 싱글 product.php 파일에 붙여넣기만 하면 결과를 볼 수 있습니다.원하는 대로 표시됩니다.

저는 최근 같은 어려움을 겪고 있으며, 모든 제품의 리뷰를 한 페이지에 출력할 수 있는 이 솔루션을 고안했습니다.

//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.";
    }
}
}

위의 기능을 기능에 추가합니다.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/에 작성했습니다.

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

「 」를하고 있는 경우print_r()$tabs선언 후 출력의 일부가 다음과 같은 것을 확인할 수 있습니다.

[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 );

이 ^는 코덱스에서 직접 가져온 것입니다.

Kishan씨는 조금 전에 글을 올렸기 때문에 조금 늦을지도 모릅니다만, 다른 사람에게 도움이 될지도 모릅니다.

제품 ID별로 shortcode로 woocommerce 리뷰를 표시하는 무료 플러그인을 개발했습니다.제품의 스키마도 삽입합니다.

확인해 보세요.무료입니다.https://shopitpress.com/plugins/sip-reviews-shortcode-woocommerce/

내장 기능을 얻기 위해 여러 번 시도했지만 실패했습니다.그리고 나는 나만의 질문을 썼다.도움이 될 수 있습니다.

$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 . "' ");

나는 일주일 동안 그것을 찾는 데 시간을 들였고 그것을 할 수 있는 쉬운 방법을 찾아냈다.

WP_Query를 사용하여 리뷰 제품을 페이지로 가져오고 싶다면echo comments_template()

커스텀 템플릿 리뷰가 필요한 경우 wp-content/plugins/woocommerce/single-product-module에서 템플릿을 사용할 수 있습니다.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(); ?>

언급URL : https://stackoverflow.com/questions/19372368/i-want-to-get-woocommerce-reviews-by-product-id-and-display-it-in-a-template

반응형