woocommercereview

How do I make sure that users can only view their reviews/comments on woocommerce products?


I used the following code in the file comments.php and it worked for posts only on blog pages ....

        <ol class="comment-list">
            <?php     $user_id = get_current_user_id();
        $user_comments = get_comments( array ( 'user_id' => $user_id ));
                wp_list_comments(
                    array(
                        'style'      => 'ol',
                        'short_ping' => true,
                        'callback'   => 'storefront_comment',
                    ),
                $user_comments );
            ?>
        </ol><!-- .comment-list -->

How can I do the same on the product pages? I tried to edit the file woocommerce/templates/single-product/review.php , but I didn't succeed.... Can you tell me how to implement this ?

Here is the code from woocommerce/templates/single-product/review.php

 * @package WooCommerce\Templates
 * @version 2.6.0
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}
?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">

    <div id="comment-<?php comment_ID(); ?>" class="comment_container">

        <?php
        /**
         * The woocommerce_review_before hook
         *
         * @hooked woocommerce_review_display_gravatar - 10
         */
        do_action( 'woocommerce_review_before', $comment );
        ?>

        <div class="comment-text">

            <?php
            /**
             * The woocommerce_review_before_comment_meta hook.
             *
             * @hooked woocommerce_review_display_rating - 10
             */
            do_action( 'woocommerce_review_before_comment_meta', $comment );

            /**
             * The woocommerce_review_meta hook.
             *
             * @hooked woocommerce_review_display_meta - 10
             */
            do_action( 'woocommerce_review_meta', $comment );

            do_action( 'woocommerce_review_before_comment_text', $comment );

            /**
             * The woocommerce_review_comment_text hook
             *
             * @hooked woocommerce_review_display_comment_text - 10
             */
            do_action( 'woocommerce_review_comment_text', $comment );

            do_action( 'woocommerce_review_after_comment_text', $comment );
            ?>

        </div>
    </div>

Solution

  • You can compare the current user id and early return if it does not match.

    On: {child-theme}/woocommerce/single-product/review.php

    Add the following checks:

     * @package WooCommerce\Templates
     * @version 2.6.0
     */
    
    if ( ! defined( 'ABSPATH' ) ) {
        exit; // Exit if accessed directly
    }
    
    $user_id = get_current_user_id();
    
    if ( !$user_id ) { return; }
    
    $comment = get_comment( get_comment_ID() );
    
    if ( $user_id !== $comment->user_id ) {
        return;
    } 
    
    ?>
    <li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">