我正在使用一段漂亮的代码来拆分订单(如果它包含任何缺货产品) 拆分 WooCommerce 订单并创建一个新订单(如果原始订单有缺货产品) 答案代码。 这对于在结账时创建第二个订单非常有效,但新订单不会链接到用户,相反,订单客户是访客。
如何将新创建的订单Customer设置为创建订单时的用户?
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();
}
// 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 );
wc_create_order()
功能中,您可以定义客户ID(和父订单),例如:
// Create new order with backorders
$backorder_order = wc_create_order( array(
'customer_id' => $order->get_customer_id(),
'parent' => $order_id,
) );
这样创建的订单将链接到用户,因此不再是“访客”。
我还简化了您当前的代码。请尝试以下方法:
add_action( 'woocommerce_checkout_order_processed', 'split_checkout_order_with_backorder_items', 10, 3 );
function split_checkout_order_with_backorder_items( $order_id, $posted_data, $order ) {
$has_backorder_items = false; // Initialize
// Loop through order items
foreach ( $order->get_items() as $item_id => $item ) {
$product = $item->get_product();
// Is item is on backorder?
if ( $product->is_on_backorder() ) {
// Create new order with the backorder item
$sub_order = wc_create_order( array(
'customer_id' => $order->get_customer_id(),
'parent' => $order_id,
));
$sub_order->add_product( $product, $item->get_quantity() ); // Add item on newly created order
$order->remove_item( $item_id ); // Remove item from current Order
$has_backorder_items = true;
$sub_order->set_currency( $order->get_currency() );
$sub_order->set_payment_method( $order->get_payment_method() );
$sub_order->set_address( $order->get_address('billing'), 'billing' );
$sub_order->set_address( $order->get_address('shipping'), 'shipping' );
$sub_order->add_order_note( sprintf( __('Automated backorder. Created from the original order ID: %d'), $order_id ) );
// Calculate totals, set status and save
$sub_order->calculate_totals();
$sub_order->update_status( 'processing' );
}
}
// If current order had backordered items
if ( $has_backorder_items ) {
// Recalculate and save current order
$order->calculate_totals();
$order->save();
}
}
代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。