为 WooCommerce 购物车中的每个项目创建单独的订单
Create individual orders for each item in WooCommerce cart
我基本上需要能够为购物车中的每件商品创建多个订单,但保留默认的结账流程。
对于用户来说,看起来他们正在结账多个项目,但在后端,它会创建多个订单。 I found an article 这几乎与我需要做的事情有关。
我假设我需要删除单击“立即付款”按钮时触发的操作,以防止自动创建订单并手动创建订单。我可以使用以下过滤器访问购物车信息,也许这是一个起点?
add_filter( 'woocommerce_checkout_create_order', 'wp_handle_multiple_orders', 10, 1 );
function wp_handle_multiple_orders( $order ) {
global $woocommerce;
print_r(count(WC()->cart->get_cart()));
// Handle creation of multiple orders
}
或者我挂钩到 checkout_process:
add_action( 'woocommerce_checkout_process', 'wp_handle_multiple_orders' ) );
我知道这个过程并不理想,需要考虑许多不同的事情,但希望有另一种方法可以实现这一点。任何链接将不胜感激!
这个答案包含:
- 只保留原始订单中的 1 个产品,删除此订单中的其他产品。
- 将已移除的产品(分别)放入新订单中。
- 通过在代码中添加注释标签进行说明,如果需要,可以轻松地进一步扩展代码
function action_woocommerce_checkout_order_processed( $order_id, $posted_data, $order ) {
// Get order currency
$order_currency = $order->get_currency();
// Get order payment method
$order_payment_gateway = $order->get_payment_method();
// Address
$order_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()
);
// Shipping
$order_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()
);
// Counter
$counter = 1;
// Loop through order items
foreach ( $order->get_items() as $item_id => $item ) {
// From the 2nd product
if ( $counter >= 2 ) {
// Get the WC_Product Object
$product = $item->get_product();
// Get product quantity
$product_quantity = $item->get_quantity();
// Create new order
$new_order = wc_create_order();
// Add product to new order
$new_order->add_product( $product, $product_quantity );
// Set addresses
$new_order->set_address( $order_address, 'billing' );
$new_order->set_address( $order_shipping, 'shipping' );
// Set the correct currency and payment gateway
$new_order->set_currency( $order_currency );
$new_order->set_payment_method( $order_payment_gateway );
// Calculate totals
$new_order->calculate_totals();
// Set order note with original ID
$new_order->add_order_note( 'Automated order. Created from the original order ID: ' . $order_id );
// Set correct status
$new_order->update_status( 'processing' );
// Delete product from original order
$order->remove_item( $item_id );
}
// Counter = counter + 1
$counter++;
}
// Re-calculate & save
$order->calculate_totals();
$order->save();
}
add_action( 'woocommerce_checkout_order_processed', 'action_woocommerce_checkout_order_processed', 10, 3 );
我基本上需要能够为购物车中的每件商品创建多个订单,但保留默认的结账流程。
对于用户来说,看起来他们正在结账多个项目,但在后端,它会创建多个订单。 I found an article 这几乎与我需要做的事情有关。
我假设我需要删除单击“立即付款”按钮时触发的操作,以防止自动创建订单并手动创建订单。我可以使用以下过滤器访问购物车信息,也许这是一个起点?
add_filter( 'woocommerce_checkout_create_order', 'wp_handle_multiple_orders', 10, 1 );
function wp_handle_multiple_orders( $order ) {
global $woocommerce;
print_r(count(WC()->cart->get_cart()));
// Handle creation of multiple orders
}
或者我挂钩到 checkout_process:
add_action( 'woocommerce_checkout_process', 'wp_handle_multiple_orders' ) );
我知道这个过程并不理想,需要考虑许多不同的事情,但希望有另一种方法可以实现这一点。任何链接将不胜感激!
这个答案包含:
- 只保留原始订单中的 1 个产品,删除此订单中的其他产品。
- 将已移除的产品(分别)放入新订单中。
- 通过在代码中添加注释标签进行说明,如果需要,可以轻松地进一步扩展代码
function action_woocommerce_checkout_order_processed( $order_id, $posted_data, $order ) {
// Get order currency
$order_currency = $order->get_currency();
// Get order payment method
$order_payment_gateway = $order->get_payment_method();
// Address
$order_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()
);
// Shipping
$order_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()
);
// Counter
$counter = 1;
// Loop through order items
foreach ( $order->get_items() as $item_id => $item ) {
// From the 2nd product
if ( $counter >= 2 ) {
// Get the WC_Product Object
$product = $item->get_product();
// Get product quantity
$product_quantity = $item->get_quantity();
// Create new order
$new_order = wc_create_order();
// Add product to new order
$new_order->add_product( $product, $product_quantity );
// Set addresses
$new_order->set_address( $order_address, 'billing' );
$new_order->set_address( $order_shipping, 'shipping' );
// Set the correct currency and payment gateway
$new_order->set_currency( $order_currency );
$new_order->set_payment_method( $order_payment_gateway );
// Calculate totals
$new_order->calculate_totals();
// Set order note with original ID
$new_order->add_order_note( 'Automated order. Created from the original order ID: ' . $order_id );
// Set correct status
$new_order->update_status( 'processing' );
// Delete product from original order
$order->remove_item( $item_id );
}
// Counter = counter + 1
$counter++;
}
// Re-calculate & save
$order->calculate_totals();
$order->save();
}
add_action( 'woocommerce_checkout_order_processed', 'action_woocommerce_checkout_order_processed', 10, 3 );