Woocommerce动作挂钩状态改变不触发

时间:2019-09-05 14:09:55

标签: php wordpress woocommerce

我想使用Woocommerce Webhook调用自定义函数,该函数在手动输入订单时通过管理面板更改订单状态时会发送一些信息。我有一个自定义状态,称为“发送”。因此,当用户选择“已分派”或“已取消”时,我想发送一些信息。

enter image description here

我也试图查看Webhooks,但找不到代码示例来展示如何使用Webhooks。这样,我编写了一个动作挂钩来检测状态更改,然后发送一个curl post。但是我什至无法触发状态更改钩子。因此,我已注释掉curl帖子以测试状态更改时是否可以简单地记录一些信息。我有2个请求:

1 /您能帮我弄清楚为什么动作挂钩没有被触发吗?

2 /如果这是解决此问题的最佳方法,如何使API Webhooks正常工作?

// hook not firing
add_action( 'woocommerce_order_status_cancelled',   'order_status_cancelled_action');
function order_status_cancelled_action($order_id) 
{
    debug_log( __FILE__.':'.__LINE__ . 'cancelled status hook fired');

}

// this hook is not firing
add_action( 'woocommerce_order_status_dispatched', 'order_status_dispatched_action');
function order_status_dispatched_action($order_id) 
{
    debug_log( __FILE__.':'.__LINE__ . 'dispatched status hook fired');

}

// tried to use this general hook for all status changes, but still no luck    
add_action('woocommerce_order_status_changed', 'order_status_changed_action', 99, 3); 
function order_status_changed_action ($order_id, $old_status, $new_status)
{
    debug_log( __FILE__.':'.__LINE__ . 'order status changed');

    /*
    // Below is the intended action to send order into to an endpoint 
    // but couldn't figure out how to use the Webhooks
    if ($new_status == 'dispatched' || $new_status == 'cancelled'){
        $order = new WC_Order( $order_id );
        $order_data = array(
                        'order_id'          => $order_id,
                        'status'            => $order->get_status(),
                        'cancel_reason'     => get_post_meta( $order_id, 'cancel_reason', true ),
                        'driver_name'       => get_post_meta( $order_id, 'driver_name', true )
        );
        $payload =json_encode($order_data,true);
        $curl=curl_init("http:myendpoint.php");
        curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
        // .... set other curlopts
        $result = curl_exec($curl);
    }
    */

}

0 个答案:

没有答案