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

如何将WooCommerce拆分生成的新订单关联至原订单用户?

WooCommerce补货订单关联原用户解决方案

你当前的代码能成功拆分包含补货商品的订单,但生成的新订单未关联原用户,显示为访客。只需添加一行代码,把原订单的用户ID绑定到新订单即可解决。

修改要点

原订单的用户ID可通过$order->get_customer_id()获取,调用set_customer_id()方法将其设置给新订单,就能让新订单关联到原用户账户。

修改后的完整代码

function sa_woocommerce_checkout_order_on_backorder( $order_id, $posted_data, $order ) {
    // Initialize
    $check_for_back_orders = false;

    // Loop through order items
    foreach ( $order->get_items() as $item_key => $item ) {
        // Get product
        $product = $item->get_product();

        // Product is on backorder
        if ( $product->is_on_backorder() ) {
            // Will only be executed once if the order contains back orders
            if ( $check_for_back_orders == false ) {
                $check_for_back_orders = true;

                // Create new order with backorders
                $backorder_order = wc_create_order();
                
                // 关联原用户ID
                $backorder_order->set_customer_id( $order->get_customer_id() );
            }

            // Add product to 'backorder' order
            $backorder_order->add_product( $product, $item['quantity'] );

            // Delete item from original order
            $order->remove_item( $item->get_id() );
        }
    }

    // If current order contains backorders, retrieve the necessary data from the existing order and apply it in the new order
    if ( $check_for_back_orders ) {
        // Recalculate and save original order
        $order->calculate_totals();
        $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
        $backorder_order->set_address( $address, 'billing' );
        $backorder_order->set_address( $shipping, 'shipping' );

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

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

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

        // Optional: give the new 'backorder' order the correct status
        $backorder_order->update_status( 'processing' );
    }
}
add_action( 'woocommerce_checkout_order_processed', 'sa_woocommerce_checkout_order_on_backorder', 10, 3 );

说明

在创建新订单$backorder_order = wc_create_order();之后,添加$backorder_order->set_customer_id( $order->get_customer_id() );这一行,就能把原订单的用户ID绑定到新订单上,新订单就会关联到原用户的账户,不再显示为访客。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 22:55:55