简体   繁体   English

如何在 Wordpress 中创建自定义 webhook,特别是对于 WooCommerce 操作?

[英]How can I create custom webhooks in Wordpress, particularly for WooCommerce actions?

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 设置 webhook,但问题是我不希望 webhook 在创建订单时执行,而是在付款完成时执行。 WooCommerce doesn't have any options available for this particular action. WooCommerce 没有可用于此特定操作的任何选项。 I asked this same question on the WooCommerce Slack group and I just got woocommerce_payment_complete as a response.我在 WooCommerce Slack 组上问了同样的问题,我刚刚得到了woocommerce_payment_complete作为回复。 I understand that this is one of the hooks that WooCommerce has.我知道这是 WooCommerce 拥有的钩子之一。 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?但是如何找到并修改它,以便添加自定义逻辑以在执行时将 HTTP 请求发布到我选择的端点?

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.我是 Wordpress 和 PHP 的新手,所以不幸的是我对在哪里看和做什么一无所知,所以希望得到一些指导。 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?我知道您可以修改 functions.php 文件以添加自定义逻辑,但是您知道我在哪里可以找到woocommerce_payment_complete挂钩并对其进行修改吗?

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 .通过将触发器设置为Action并将操作名称设置为woocommerce_payment_complete ,我成功地发送了支付完成的 webhook。 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?如何自定义有效负载以包含所有订单详细信息?

Hook your function the action (as your figured out) and then post any data related to that order to your custom end point...将您的 function 挂钩到操作(如您所想),然后将与该订单相关的任何数据发布到您的自定义端点......

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.你可以把它放在你的 function 文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM