2

I'm looking for a WooCommerce action hook, whenever someone submits a product review via the review/comment form. Can't find anything about this in the documentation or web.

I like to trigger a WooCommerce webhook, when that event fires, to run some other actions via external services.

So this should be something like i.e. action.woocommerce_order_status_completed, which I already use successfully.

So what I'm looking for is something like "action.woocommerce_review_created" … Any ideas?

1 Answer 1

0

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.

1
  • Works like a charm! Thanks for pointing me into the right direction! Commented Jun 30 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.