Home > other >  How can I create custom webhooks in Wordpress, particularly for WooCommerce actions?
How can I create custom webhooks in Wordpress, particularly for WooCommerce actions?

Time:09-26

I've managed to set up webhooks with WooCommerce, but the issue is that I don't want the webhook to execute when an order is created, but rather when the payment is complete. WooCommerce doesn't have any options available for this particular action. I asked this same question on the WooCommerce Slack group and I just got woocommerce_payment_complete as a response. I understand that this is one of the hooks that WooCommerce has. But how can I find and modify this so that I can add custom logic to post a HTTP request to my endpoint of choice when it executes?

I'm new to Wordpress and PHP, so I'm unfortunately quite clueless about where to look and what to do, so would appreciate some guidance. I understand that you can modify the functions.php file to add custom logic, but do you know where exactly I could find the woocommerce_payment_complete hook and modify it?

Thanks.

Update
I managed to successfully send a webhook on payment completion by setting the trigger to Action and the action name to woocommerce_payment_complete. It's now triggering when I want it to, but the payload is empty. How can I customize the payload to include all the order details?

CodePudding user response:

Hook your function the action (as your figured out) and then post any data related to that order to your custom end point...

add_action( 'woocommerce_payment_complete', 'my_function_payment_complete' );

function my_function_payment_complete( $order_id ) {
    
    $order = wc_get_order( $order_id );
    // do what ever you want with your order data.
    
}

You can either put this in your function file.

  • Related