You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Woocommerce REST API:订单SKU不全匹配时跳过整单传输

实现订单全SKU匹配才传输的解决方案

要解决部分SKU匹配就传输订单导致打包不全的问题,我们可以在遍历商品时先校验所有SKU是否都能在目标站点找到对应的产品,只有全部匹配成功才执行后续的订单传输操作。具体修改如下:

核心逻辑调整

  • 添加一个标记变量$all_skus_matched,初始设为true
  • 遍历每个商品时,检查远程请求是否成功、是否获取到有效产品
  • 若任意商品SKU不匹配或请求失败,将标记设为false并提前终止遍历
  • 仅当标记为true时,才构建订单数据并发起传输请求

修改后的完整代码

add_action( "woocommerce_order_status_processing_to_post", "transfer_order_woo_api", 9999, 3 );
function transfer_order_woo_api( $order_id, $order ) {
    $order = wc_get_order($order_id);
    $my_var = get_post_meta($order->get_id(), 'warehouse', true);
    $my_varparam = 'transfered';
    
    if ($my_var !== $my_varparam) {
        $remote_keys = "consumer_key=ck_xxx&consumer_secret=cs_xxx";
        $live_url = "https://test.com/wp-json/wc/v3/orders?{$remote_keys}";
        $line_items = array();
        $all_skus_matched = true; // 初始化全匹配标记
        
        foreach ( $order->get_items() as $item_id => $item ) {
            $quantity = $item->get_quantity();
            $product = $item->get_product();
            $product_sku = $product->get_sku();
            
            // 获取远程站点产品信息
            $remote_get = wp_remote_get("https://test.com/wp-json/wc/v3/products?sku={$product_sku}&{$remote_keys}");
            
            // 检查请求是否成功
            if (is_wp_error($remote_get) || empty($remote_get['body'])) {
                $all_skus_matched = false;
                break; // 提前终止遍历
            }
            
            $remote_product = json_decode($remote_get['body'])[0];
            
            // 检查是否找到对应产品
            if (empty($remote_product) || !isset($remote_product->id)) {
                $all_skus_matched = false;
                break; // 提前终止遍历
            }
            
            $line_item = array(
                "quantity" => $quantity,
                "product_id" => $remote_product->id
            );
            
            $line_items[] = $line_item;
        }
        
        // 仅当所有SKU都匹配时,才执行订单传输
        if ($all_skus_matched) {
            $body = array(
                'status' => 'processing',
                'meta_data' => array( 
                    array( 
                        'key' => 'carrier',
                        'value' => get_post_meta($order->get_id(), 'ywot_carrier_id', true)
                    ),
                    array( 
                        'key' => 'order_number',
                        'value' => get_post_meta($order->get_id(), '_alg_wc_custom_order_number', true)
                    ),
                    array( 
                        'key' => 'orderid',
                        'value' => $order->get_id(),
                    ),
                    array( 
                        'key' => 'shipped_date',
                        'value' => get_post_meta($order->get_id(), 'shipped_date', true)
                    ),
                    array( 
                        'key' => 'ywot_tracking_code',
                        'value' => get_post_meta($order->get_id(), 'ywot_tracking_code', true)
                    )
                ),
                'billing' => array(
                    'first_name' => $order->get_billing_first_name(),
                    'last_name' => $order->get_billing_last_name(),
                    'address1' => $order->get_billing_address_1(),
                    'city' => $order->get_billing_city(),
                    'postcode' => $order->get_billing_postcode(),
                    'country' => $order->get_billing_country(),
                    'phone' => $order->get_billing_phone(),
                ),
                'shipping' => array(
                    'first_name' => $order->get_shipping_first_name(),
                    'last_name' => $order->get_shipping_last_name(),
                    'address1' => $order->get_shipping_address_1(),
                    'city' => $order->get_shipping_city(),
                    'postcode' => $order->get_shipping_postcode(),
                    'country' => $order->get_shipping_country(),
                ),
                'line_items' => $line_items,
            );
            
            $raw_response = wp_remote_post( $live_url, 
                array(
                    'headers' => array( 'Content-Type' => 'application/json' ),
                    'timeout' => 30,                    
                    'body' => json_encode( $body ),
                )
            );
            
            $order->update_meta_data( 'warehouse', 'transfered' );
            $order->save();
        }
        // 若SKU不全匹配,不执行任何传输和元数据更新操作
    }
}

关键修改点说明

  1. 新增全匹配标记:用$all_skus_matched跟踪所有商品SKU的匹配状态
  2. 请求错误处理:通过is_wp_error判断远程请求是否失败,避免因网络问题导致误判
  3. 产品存在校验:检查解析后的远程产品是否有效,确保SKU确实存在于目标站点
  4. 条件执行传输:只有所有SKU都匹配成功,才会构建订单数据并发起传输请求,同时更新订单的warehouse元数据

内容的提问来源于stack exchange,提问作者Lubo Masura

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 08:24:16