根据Woocommerce 3中的订单状态将订单发送到送货服务

时间:2018-08-29 08:11:08

标签: php wordpress if-statement woocommerce orders

我的订单会自动发送到外部送货服务。但是,当订单状态更改时,它会再次发送到外部交货服务。如何解决?

add_action( 'woocommerce_order_status_changed', 'order_stock_reduction_based_on_status', 20, 4 );
function order_stock_reduction_based_on_status( $order_id, $old_status, $new_status, $order ){
    // Only for 'processing' and 'hold-on' order statuses change
    if ( $new_status == 'processing' || $new_status == 'hold-on' ) {
        // Checking if this has already been done avoiding reload
        if (get_post_meta($order_id, 'delivery_order_id', true)) {
            return; // Exit if already processed
        }
    }

    $order_data = $order->get_data();

    // Send data
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://app.example.com/api/index.php?new_order");
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $result = curl_exec($ch);
    curl_close($ch);

    $decoded = (array) json_decode($result);

    // Output
    if( isset($decoded['result']) && $decoded['result'] == 'success' && isset($decoded['order_id']) && !empty($decoded['order_id']) ){
        update_post_meta( $order_id, 'delivery_order_id', esc_attr( $decoded['order_id'] ) );
    }
}

1 个答案:

答案 0 :(得分:1)

我认为问题很简单。应该首先关闭第一条IF语句,以避免出现重复问题。

所以您的代码将是:

add_action( 'woocommerce_order_status_changed', 'order_stock_reduction_based_on_status', 20, 4 );
function order_stock_reduction_based_on_status( $order_id, $old_status, $new_status, $order ){
    // Only for 'processing' and 'hold-on' order statuses change
    if ( $new_status == 'processing' || $new_status == 'hold-on' ) {
        // Checking if this has already been done avoiding reload
        $delivery_order_id = get_post_meta($order_id, 'delivery_order_id', true);
        if ( ! empty( $delivery_order_id  ) ) {
            return; // Exit if already processed
        }

        $order_data = $order->get_data();

        // Send data
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://app.example.com/api/index.php?new_order");
        curl_setopt($ch, CURLOPT_FAILONERROR, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $result = curl_exec($ch);
        curl_close($ch);

        $decoded = (array) json_decode($result);

        // Output
        if( isset($decoded['result']) && $decoded['result'] == 'success' && isset($decoded['order_id']) && !empty($decoded['order_id']) ){
            update_post_meta( $order_id, 'delivery_order_id', esc_attr( $decoded['order_id'] ) );
        }
    } // <==  <==  <==  <==  Closing bracket HERE
}

现在应该可以正常工作了(我希望)。