首页 > 解决方案 > 根据 Woocommerce 3 中的订单状态将订单发送到送货服务

问题描述

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

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

标签: phpwordpressif-statementwoocommerceorders

解决方案


我认为问题很简单。第一条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
}

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


推荐阅读