How to find which product variation is bought by current user?

In Woocommerce’s reviews there is a label for ‘verified owner’. But i want to add the variation he bought. For example if we have a variation named ‘Red’ it should be ‘Verfied Owner for Red’. How to do that?

image

In this image we have a badge of ‘verified owner’. This should be ‘verified owner for variation_1’.

I can change the template, but i need to find if the user had commented for current variation.

Note that i have separate pages for variations. So when user is commenting, he is always commenting for a specific variation and that variation cannot be changed by comboboxes.

To display the “Verified Owner for Variation_1” label in WooCommerce product reviews, you’ll need to modify the review template and retrieve the variation information for the reviewed product.

1. Find the Review Template:

Locate the review template file in your WooCommerce theme’s templates directory. It’s typically named something like single-product-reviews.php .

2. Modify the Verified Owner Label:

Within the template, find the code that generates the “Verified Owner” label. This code usually checks if the reviewer is the product’s owner. Modify it to include the variation information:

PHP

<?php
// Find the variation ID for the reviewed product
$variation_id = get_post_meta( $comment->comment_post_ID, '_product_variation', true );

// Check if the reviewer is the product's owner and if the variation matches
$is_verified_owner = wc_customer_bought_product( $user_id, $product_id, $variation_id );

if ( $is_verified_owner ) {
    echo '<span class="verified-owner">Verified Owner for Variation ' . $variation_id . '</span>';
}
?>

3. Retrieve Variation Information:

  • The get_post_meta() function retrieves the variation ID for the reviewed product, stored as a meta key _product_variation .
  • The wc_customer_bought_product() function checks if the user is the product’s owner and if the variation matches the one reviewed.

4. Customize the Label:

  • You can further customize the label by modifying the text “Verified Owner for Variation” to match your desired format.

Additional Considerations:

  • Ensure that the $variation_id variable is correctly retrieved and that the wc_customer_bought_product() function is working as expected.
  • If you have multiple variations for a product, you might need to adjust the logic to display the correct variation label based on the reviewed product’s variation.
  • Consider using a more readable format for variation IDs (e.g., “Red” instead of “1”) if applicable.

By following these steps, you should be able to display the “Verified Owner for Variation_1” label in your WooCommerce product reviews, providing additional context for customers.