简体   繁体   English

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

[英]Send order to delivery service based on order status in Woocommerce 3

My orders are automatically sent to the external delivery service.我的订单会自动发送到外部送货服务。 But when the status of the order changes, it is again sent to the external delivery service.但是当订单状态发生变化时,它会再次发送到外部交付服务。 How to fix it?如何解决?

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'] ) );
    }
}

The problem is very simple I think.我想这个问题很简单。 The First IF statement should be closed at the end instead, to avoid this repetition issue.第一个IF语句应该在最后关闭,以避免这种重复问题。

So your code will be:所以你的代码将是:

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
}

It should just work fine now (I hope).它现在应该可以正常工作(我希望)。

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

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