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

如何在WooCommerce中按指定配送日期拆分订单并添加商品

WooCommerce按配送日期拆分订单解决方案

问题背景

我是WooCommerce新手,希望按特定配送日期拆分订单。目前使用product_type作为条件时,能成功将商品添加到新创建的订单,但实际需要以acf-delivery_date字段作为判断条件。我已通过代码将商品的自定义字段(包括acf-delivery_date)保存至order_itemmeta数据库表,并修改了订单商品元数据的前台显示标签。在订单拆分代码中,我尝试了多种条件判断写法,但均无法实现按指定配送日期添加商品到新订单的功能,现求助正确的代码实现方式。

已实现的自定义字段保存代码

// Save and display "Custom Field for Simple Products" on order items everywhere
add_filter( 'woocommerce_checkout_create_order_line_item', 'action_wc_checkout_create_order_line_item_2', 10, 4 );
function action_wc_checkout_create_order_line_item_2( $item, $cart_item_key, $values, $order ) {
    // Get the Custom Field
    $calories = $values['data']->get_meta( 'acf-calories' );

    if( ! empty( $calories ) ) {
        // Save it and display it
        $item->update_meta_data( __( 'acf-calories', 'woocommerce' ), $calories );
    }   
     $delivery_date = $values['data']->get_meta( 'acf-delivery_date' );

    if( ! empty( $delivery_date ) ) {
        // Save it and display it
        $item->update_meta_data( __( 'acf-delivery_date', 'woocommerce' ), $delivery_date );
    }
}
    
// Change displayed label for specific custom order item meta keys
add_filter('woocommerce_order_item_display_meta_key', 'filter_wc_order_item_display_meta_key', 10, 3 );
function filter_wc_order_item_display_meta_key( $display_key, $meta, $item ) {

    // Change displayed label for specific order item meta key
    if( $item->get_type() === 'line_item' ) {
        if( $meta->key === 'acf-calories' ) {
            $display_key = __("Calories", "woocommerce");
        }
        if( $meta->key === 'acf-delivery_date' ) {
            $display_key = __("Delivery Date", "woocommerce");
        }
    }
    return $display_key;
}

待修改的订单拆分代码(需调整条件部分)

function action_woocommerce_checkout_order_processed( $order_id, $posted_data, $order ) {
    $order = wc_get_order( $order_id);
    $new_order = wc_create_order();
    $target_delivery_date = '05-30-2023';
    // Loop through order items
    foreach ( $order->get_items() as $item_id => $item ) {
        // Get product
        $product = $item->get_product();
        $delivery_date = $item->get_meta('acf-delivery_date');
        $product_quantity = $item->get_quantity();
        $parent_id = $item->get_meta('parent_id');
        $product_string = $item->get_meta('wooco_ids');
    
        // 此处为需要修改的条件判断部分
        if ( $product->is_type( 'simple' ))  {
            $new_order->add_product( $product, $product_quantity );
       } 
    }   
        
    // Obtain necessary information
    // Get address
    $address = array(
        'first_name' => $order->get_billing_first_name(),
        'last_name'  => $order->get_billing_last_name(),
        'email'      => $order->get_billing_email(),
        'phone'      => $order->get_billing_phone(),
        'address_1'  => $order->get_billing_address_1(),
        'address_2'  => $order->get_billing_address_2(),
        'city'       => $order->get_billing_city(),
        'state'      => $order->get_billing_state(),
        'postcode'   => $order->get_billing_postcode(),
        'country'    => $order->get_billing_country()
    );

    // Get shipping
    $shipping = array(
        'first_name' => $order->get_shipping_first_name(),
        'last_name'  => $order->get_shipping_last_name(),
        'address_1'  => $order->get_shipping_address_1(),
        'address_2'  => $order->get_shipping_address_2(),
        'city'       => $order->get_shipping_city(),
        'state'      => $order->get_shipping_state(),
        'postcode'   => $order->get_shipping_postcode(),
        'country'    => $order->get_shipping_country()
    );
    
    // Get order currency
    $currency = $order->get_currency();

    // Get order payment method
    $payment_gateway = $order->get_payment_method();
    
    // Required information has been obtained, assign it to the 'backorder' order
    // Set address
    $new_order->set_address( $address, 'billing' );
    $new_order->set_address( $shipping, 'shipping' );

    // Set the correct currency and payment gateway
    $new_order->set_currency( $currency );
    $new_order->set_payment_method( $payment_gateway );

    // Calculate totals
    $new_order->calculate_totals();

    // Set order note with original ID
    $new_order->add_order_note( 'Automated backorder. Created from the original order ID: ' . $order_id );

    $new_order->update_status( 'pending' );
}

add_action( 'woocommerce_checkout_order_processed', 'action_woocommerce_checkout_order_processed', 10, 3 );

无效的条件写法尝试

以下是我尝试过但无效的条件判断写法:

if ( $product( 'acf-delivery_date' => '05-30-2023')) ) {
  $new_order->add_product( $product, $product_quantity );
}
if ( $item( 'acf-delivery_date' => '05-30-2023')) ) {
  $new_order->add_product( $product, $product_quantity );
}   
if ( $product( 'acf-delivery_date' == '05-30-2023')) ) {
  $new_order->add_product( $product, $product_quantity );
}
if ( $item( 'acf-delivery_date' == '05-30-2023')) ) {
  $new_order->add_product( $product, $product_quantity );
}   

正确的条件判断实现

你已经通过$delivery_date = $item->get_meta('acf-delivery_date');获取到了订单商品的配送日期,只需要直接比较这个变量和目标日期即可。注意要保证日期格式完全一致(比如ACF字段存储的是MM-DD-YYYY还是YYYY-MM-DD)。

基础写法(格式完全匹配时)

将原代码中的条件判断替换为:

// 直接匹配指定配送日期
if ( $delivery_date === $target_delivery_date ) {
    $new_order->add_product( $product, $product_quantity );
    // 可选:如果需要从原订单中移除该商品,添加以下两行
    // $order->remove_item( $item_id );
    // $order->save();
}

推荐写法(统一日期格式,避免格式不匹配问题)

如果不确定存储的日期格式,建议用DateTime统一格式后再比较,避免因格式差异导致的判断失败:

if ( ! empty( $delivery_date ) ) {
    // 根据实际存储的日期格式调整第一个参数(比如ACF设置的是"m/d/Y"就改成'm/d/Y')
    $stored_date = DateTime::createFromFormat('m-d-Y', $delivery_date);
    $target_date = DateTime::createFromFormat('m-d-Y', $target_delivery_date);
    
    if ( $stored_date && $target_date && $stored_date->format('Y-m-d') === $target_date->format('Y-m-d') ) {
        $new_order->add_product( $product, $product_quantity );
        // 可选:移除原订单中的对应商品
        // $order->remove_item( $item_id );
        // $order->save();
    }
}

修改后的完整订单拆分代码

替换条件部分后的完整代码如下:

function action_woocommerce_checkout_order_processed( $order_id, $posted_data, $order ) {
    $order = wc_get_order( $order_id);
    $new_order = wc_create_order();
    $target_delivery_date = '05-30-2023';
    // Loop through order items
    foreach ( $order->get_items() as $item_id => $item ) {
        // Get product
        $product = $item->get_product();
        $delivery_date = $item->get_meta('acf-delivery_date');
        $product_quantity = $item->get_quantity();
        $parent_id = $item->get_meta('parent_id');
        $product_string = $item->get_meta('wooco_ids');
    
        // 正确的条件判断:匹配指定配送日期
        if ( $delivery_date === $target_delivery_date ) {
            $new_order->add_product( $product, $product_quantity );
            // 可选:从原订单移除该商品
            // $order->remove_item( $item_id );
        } 
    }   

    // 保存原订单的修改(如果移除了商品)
    // $order->save();
        
    // Obtain necessary information
    // Get address
    $address = array(
        'first_name' => $order->get_billing_first_name(),
        'last_name'  => $order->get_billing_last_name(),
        'email'      => $order->get_billing_email(),
        'phone'      => $order->get_billing_phone(),
        'address_1'  => $order->get_billing_address_1(),
        'address_2'  => $order->get_billing_address_2(),
        'city'       => $order->get_billing_city(),
        'state'      => $order->get_billing_state(),
        'postcode'   => $order->get_billing_postcode(),
        'country'    => $order->get_billing_country()
    );

    // Get shipping
    $shipping = array(
        'first_name' => $order->get_shipping_first_name(),
        'last_name'  => $order->get_shipping_last_name(),
        'address_1'  => $order->get_shipping_address_1(),
        'address_2'  => $order->get_shipping_address_2(),
        'city'       => $order->get_shipping_city(),
        'state'      => $order->get_shipping_state(),
        'postcode'   => $order->get_shipping_postcode(),
        'country'    => $order->get_shipping_country()
    );
    
    // Get order currency
    $currency = $order->get_currency();

    // Get order payment method
    $payment_gateway = $order->get_payment_method();
    
    // Required information has been obtained, assign it to the 'backorder' order
    // Set address
    $new_order->set_address( $address, 'billing' );
    $new_order->set_address( $shipping, 'shipping' );

    // Set the correct currency and payment gateway
    $new_order->set_currency( $currency );
    $new_order->set_payment_method( $payment_gateway );

    // Calculate totals
    $new_order->calculate_totals();

    // Set order note with original ID
    $new_order->add_order_note( 'Automated backorder. Created from the original order ID: ' . $order_id );

    $new_order->update_status( 'pending' );
}

add_action( 'woocommerce_checkout_order_processed', 'action_woocommerce_checkout_order_processed', 10, 3 );

内容的提问来源于stack exchange,提问作者Lady Murasaki

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 22:47:01