Magento multiple order on one checkout or order splitting
It is doable quite easily with a rewrite of the checkout/type_onepage
model.
In that class override the saveOrder()
method as follows:
public function saveOrder()
{
$quote = $this->getQuote();
// First build an array with the items split by vendor
$sortedItems = array();
foreach ($quote->getAllItems() as $item) {
$vendor = $item->getProduct()->getVendor(); // <- whatever you need
if (! isset($sortedItems[$vendor])) {
$sortedItems[$vendor] = $item;
}
}
foreach ($sortedItems as $vendor => $items) {
// Empty quote
foreach ($quote->getAllItems() as $item) {
$quote->getItemsCollection()->removeItemByKey($item->getId());
}
foreach ($items as $item) {
$quote->addItem($item);
}
// Update totals for vendor
$quote->setTotalsCollectedFlag(false)->collectTotals();
// Delegate to parent method to place an order for each vendor
parent::saveOrder();
}
return $this;
}
But be aware that in Magento a payment is associated with an invoice, and each invoice is associated with an order.
In consequence this means that as soon as you have multiple orders, you will also have split the payments. So this is only feasible if the payment method doesn't require user interaction during the payment.
UPDATE:
The orginal answer delegated to parent::save()
which had to be parent:saveOrder()
. It is fixed in the example code now.