Last updated on October 18, 2023

WooCommerce customer reviews and ratings

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Encourage customer engagement with reviews and ratings in WooCommerce.

The code snippet below demonstrates how to display the average rating and total number of reviews for a WooCommerce product:

/**
 * Display average rating and total reviews for a product.
 *
 * @param int $product_id The ID of the product.
 */
function wpsnippets_display_product_reviews( $product_id ) {
    $average_rating = wc_get_average_rating( $product_id );
    $review_count = get_comments_number( $product_id );

    echo '<div class="product-reviews">';
    echo '<p>Average Rating: ' . $average_rating . '</p>';
    echo '<p>Total Reviews: ' . $review_count . '</p>';
    echo '</div>';
}

To use this code snippet, you can call the wpsnippets_display_product_reviews() function and pass the product ID as a parameter. It will then retrieve the average rating and total number of reviews for that product using WooCommerce functions (wc_get_average_rating() and get_comments_number()), and display them within a <div> element.

This code snippet can be useful when you want to show the average rating and total number of reviews for a product on its single product page or in a product listing. It provides valuable information to potential customers and helps them make informed purchasing decisions.

Examples

Example 1: Displaying Average Rating on Product Page

This use case demonstrates how to display the average rating for a WooCommerce product on the product page.

/**
 * Display average rating on product page.
 */
function wpsnippets_display_average_rating() {
    global $product;

    if ( $product->get_average_rating() ) {
        echo '<div class="average-rating">' . $product->get_average_rating() . '</div>';
    }
}
add_action( 'woocommerce_single_product_summary', 'wpsnippets_display_average_rating', 10 );

This code example uses the woocommerce_single_product_summary action hook to add a custom function that displays the average rating of the current product. The get_average_rating() method is used to retrieve the average rating value, and it is then echoed within a <div> element.

Example 2: Adding Custom Rating Field to Checkout

This use case demonstrates how to add a custom rating field to the WooCommerce checkout page.

/**
 * Add custom rating field to checkout.
 */
function wpsnippets_add_custom_rating_field( $checkout ) {
    echo '<div id="custom_rating_field">';
    woocommerce_form_field( 'custom_rating', array(
        'type'        => 'select',
        'class'       => array( 'form-row-wide' ),
        'label'       => __( 'Rating', 'woocommerce' ),
        'required'    => true,
        'options'     => array(
            ''   => __( 'Select a rating', 'woocommerce' ),
            '1'  => __( '1 star', 'woocommerce' ),
            '2'  => __( '2 stars', 'woocommerce' ),
            '3'  => __( '3 stars', 'woocommerce' ),
            '4'  => __( '4 stars', 'woocommerce' ),
            '5'  => __( '5 stars', 'woocommerce' ),
        )
    ), $checkout->get_value( 'custom_rating' ) );
    echo '</div>';
}
add_action( 'woocommerce_after_order_notes', 'wpsnippets_add_custom_rating_field' );

This code example uses the woocommerce_after_order_notes action hook to add a custom function that displays a select field for the rating on the checkout page. The woocommerce_form_field() function is used to generate the HTML for the field, with options for different rating values.

Example 3: Customizing Review Submission Form

This use case demonstrates how to customize the review submission form in WooCommerce.

/**
 * Customize review submission form.
 */
function wpsnippets_customize_review_form( $fields ) {
    $fields['comment_field']['label'] = __( 'Your Review', 'woocommerce' );
    $fields['comment_field']['placeholder'] = __( 'Write your review here...', 'woocommerce' );
    $fields['comment_field']['rows'] = 5;
    $fields['rating']['required'] = false;

    return $fields;
}
add_filter( 'woocommerce_product_review_comment_form_args', 'wpsnippets_customize_review_form' );

This code example uses the woocommerce_product_review_comment_form_args filter to modify the arguments for the review submission form. The function wpsnippets_customize_review_form() customizes the label, placeholder, and number of rows for the review comment field. It also sets the rating field as not required by setting the required parameter to false.

Last updated on October 18, 2023. Originally posted on January 14, 2024.

Leave a Reply

Your email address will not be published. Required fields are marked *