As we know WooCommerce product reviews are essentially WordPress comments
with a specific comment_type
.
Firstly we hook into the Comment Submission and then we will check If the Comment is a WooCommerce Product Review
and then we trigger
our Custom Action
.
Could you please try to add this to your theme’s functions.php or in your custom plugin.
function customized_woocommerce_review_comment_hook( $comment_id, $comment_approved ) {
// Here we are checking if the comment is approved and if it's a WooCommerce product review.
if ( $comment_approved && get_comment_type( $comment_id ) === 'review' ) {
// You can add your custom action here
do_action( 'woocommerce_review_created', $comment_id );
// Optionally, we can also add a webhook trigger here.
// For example, here we are sending data to an external service.
$comment = get_comment( $comment_id );
$product_id = $comment->comment_post_ID;
// Here we are preparing the data to send.
$data = [
'product_id' => $product_id,
'review_id' => $comment_id,
'review_content' => $comment->comment_content,
'review_author' => $comment->comment_author,
// Add more fields as per the need.
];
// Here we are sending the data to the webhook URL.
$webhook_url = 'https://your-webhook-url.com'; //We need to replace this with our webhook URL.
wp_remote_post($webhook_url, [
'body' => json_encode($data),
'headers' => [
'Content-Type' => 'application/json',
],
]);
}
}
add_action( 'comment_post', 'customized_woocommerce_review_comment_hook', 10, 2 );
Hope this will helps to you.