Send Email to Customers Who Leave Reviews in WooCommerce

This custom code snippet enables you to automatically send emails to anyone who leaves a starred review in WooCommerce.

You can send emails based on any rating between 1 to 5 stars. In this example, the code sends an email you can customize, to anyone who leaves a 5 star review.

Copy and paste the following PHP code to the end of your child themes functions file or custom functionality/code snippet plugin.

add_action('comment_post', 'send_email_after_starred_review', 10, 2);

function send_email_after_starred_review($comment_id, $comment_approved) {

    if ($comment_approved !== 1) {
        return;
    }

    $comment = get_comment($comment_id);
    $rating = intval(get_comment_meta($comment_id, 'rating', true));

    // Only proceed if the rating is 5 stars
    if ($rating === 5) {
        $comment_author_email = $comment->comment_author_email;
        $product_id = $comment->comment_post_ID;
        $product = wc_get_product($product_id);
        $product_name = $product->get_name();

        $subject = 'Thank you for your 5-star review!';
        $message = "Dear Customer,\n\nThank you for leaving a 5-star review on our product: $product_name. We appreciate your feedback and support.\n\nBest regards,\nYour Company Name";

        // Send the email
        wp_mail($comment_author_email, $subject, $message);
    }
}

Code Modification

Swap out the 5 in the code to any other rating between 1 and 5.

Swap out the content between the “” double quotes to personalize your email message.

The code can be modified and extended to do anything you need.

Need More Coding Help?

Book a Consultation