Вы можете подключиться к woocommerce_payment_complete
, который принимает $order_id
, затем вы можете пройтись по пунктам и решить, создавать ли еще один заказ. Затем вы можете разделить элементы в новый порядок. Вот (непроверенный) пример:
define('PACKAGE_PRODUCT_ID', 1010);
add_action('woocommerce_payment_complete', 'order_splitter', 100, 1);
function order_splitter($order_id){
$completed_order = new WC_Order($order_id);
$item_splitted = false;
$address = array(
'first_name' => $completed_order->get_billing_first_name(),
'last_name' => $completed_order->get_billing_last_name(),
'company' => '',
'email' => $completed_order->get_billing_email(),
'phone' => $completed_order->get_billing_phone(),
'address_1' => $completed_order->get_billing_address_1(),
'address_2' => $completed_order->get_billing_address_2(),
'city' => $completed_order->get_billing_city(),
'state' => $completed_order->get_billing_state(),
'postcode' => $completed_order->get_billing_postcode(),
'country' => $completed_order->get_billing_country()
);
foreach($completed_order->get_items() as $item){
if (!$item_splitted && $item->get_product_id() === PACKAGE_PRODUCT_ID) {
//create new order
$new_order_args = array(
'customer_id' => $completed_order->get_customer_id(),
'status' => 'wc-pending',
);
$new_order = wc_create_order($new_order_args);
$product_to_add = wc_get_product(PACKAGE_PRODUCT_ID);
$new_order->add_product($product_to_add, 1, array());
$new_order->set_address($address, 'billing');
$new_order->set_address($address, 'shipping');
$new_order->update_status('wc-processing');
$new_order->add_order_note('This order created automatically');
$new_order->save();
$completed_order->remove_item($item->get_id());
$item_splitted = true;
} else if ($item_splitted && $item['product_id'] === PACKAGE_PRODUCT_ID){
# This will ensure every 2 products are splitted (skipping the 2nd one)
$item_splitted = false;
continue;
}
}
}