I had to split orders based on the vendor/seller selling that product.
Like, for example, if someone orders Product1, Product2 and Product3, where Product1 and Product2 are sold by Vendor1 and Product3 is sold by Vendor2.
Then, 2 orders with different Order Ids must be created where the Order1 will have details regarding the order of Product1 and Product2 and the Order2 will have the details regarding the order of Product3.
I changed the saveOrder()
function. It now splits the order but the calculation of totals is not done in the correct way. The total in all the orders are same, i.e., of the complete order and not of the order of the particular vendor.
saveOrder()
public function saveOrder()
{
$this->validate();
$isNewCustomer = false;
switch ($this->getCheckoutMethod())
{
case self::METHOD_GUEST:
$this->_prepareGuestQuote();
break;
case self::METHOD_REGISTER:
$this->_prepareNewCustomerQuote();
$isNewCustomer = true;
break;
default:
$this->_prepareCustomerQuote();
break;
}
$cart = $this->getQuote();
$key=0;
foreach($cart->getAllItems() as $item)
{
$i=$i+1;
$flag=0;
$seller= Mage::getModel('catalog/product')->load($item->getProductId())->getData('sold_by');
foreach($sellers as $slr)
{
if($slr==$seller)
{
$flag=1;
}
}
if($flag==0)
{
$sellers[$i]=$seller;
}
}
foreach($sellers as $seller)
{
foreach ($cart->getAllItems() as $item)
{
$slr= Mage::getModel('catalog/product')->load($item->getProductId())->getData('sold_by');
if($slr==$seller)
{
$key= $key+1;
$temparray[$key]['product_id']= $item->getProduct()->getId();
$temparray[$key]['qty']= $item->getQty();
$temparray[$key]['slr1']= $slr;
$cart->removeItem($item->getId());
}
}
$cart->setSubtotal(0);
$cart->setBaseSubtotal(0);
$cart->setSubtotalWithDiscount(0);
$cart->setBaseSubtotalWithDiscount(0);
$cart->setGrandTotal(0);
$cart->setBaseGrandTotal(0);
$cart->setTotalsCollectedFlag(true);
$cart->collectTotals();
}
$cart->save();
$productObj=array();
$quoteItem=array();
foreach($sellers as $seller)
{
$i=-1;
$customer_id = Mage::getSingleton('customer/session')->getId();
$store_id = Mage::app()->getStore()->getId();
$customerObj = Mage::getModel('customer/customer')->load($customer_id);
$quoteObj = $cart;
$sub=0;
$storeObj = $quoteObj->getStore()->load($store_id);
$quoteObj->setStore($storeObj);
$productModel = Mage::getModel('catalog/product');
foreach ($temparray as $key => $item)
{
$slr= $item['slr1'];
if($slr==$seller)
{
$i=$i+1;
$productObj[$i] = $productModel->load($item['product_id']);
$quoteItem[$i] = Mage::getModel('sales/quote_item')->setProduct($productObj[$i]);
$quoteItem[$i]->setBasePrice($productObj[$i]->getFinalPrice());
$quoteItem[$i]->setPriceInclTax($productObj[$i]->getFinalPrice());
$quoteItem[$i]->setData('original_price', $productObj[$i]->getPrice());
$quoteItem[$i]->setData('price', $productObj[$i]->getPrice());
$sub+=$quoteItem[$i]->setRowTotal($productObj[$i]->getFinalPrice()*$item['qty']);
$quoteItem[$i]->setQuote($quoteObj);
$quoteItem[$i]->setQty($item['qty']);
$quoteItem[$i]->setStoreId($store_id);
$quoteObj->addItem($quoteItem[$i]);
}
}
$quoteObj->setBaseSubtotal($productObj[$i]->getFinalPrice()+700);
$quoteObj->setSubtotal($productObj[$i]->getFinalPrice()+800);
$quoteObj->setBaseGrandTotal($productObj[$i]->getFinalPrice()+900);
$quoteObj->setGrandTotal($productObj[$i]->getFinalPrice()+1000);
$quoteObj->setStoreId($store_id);
$quoteObj->collectTotals();
$quoteObj->save();
$this->_quote=$quoteObj;
$service = Mage::getModel('sales/service_quote', $quoteObj);
$service->submitAll();
if ($isNewCustomer)
{
try
{
$this->_involveNewCustomer();
}
catch (Exception $e)
{
Mage::logException($e);
}
}
$this->_checkoutSession->setLastQuoteId($quoteObj->getId())
->setLastSuccessQuoteId($quoteObj->getId())
->clearHelperData();
$order = $service->getOrder();
if ($order)
{
Mage::dispatchEvent('checkout_type_onepage_save_order_after',
array('order'=>$order, 'quote'=>$quoteObj));
$quoteObj->removeAllItems();
$quoteObj->setTotalsCollectedFlag(true);
$quoteObj->collectTotals();
}
/**
* a flag to set that there will be redirect to third party after confirmation
* eg: paypal standard ipn
*/
$redirectUrl = $this->getQuote()->getPayment()->getOrderPlaceRedirectUrl();
/**
* we only want to send to customer about new order when there is no redirect to third party
*/
if (!$redirectUrl && $order->getCanSendNewEmailFlag())
{
try
{
$order->sendNewOrderEmail();
}
catch (Exception $e)
{
Mage::logException($e);
}
}
// add order information to the session
$this->_checkoutSession->setLastOrderId($order->getId())
->setRedirectUrl($redirectUrl)
->setLastRealOrderId($order->getIncrementId());
// as well a billing agreement can be created
$agreement = $order->getPayment()->getBillingAgreement();
if ($agreement)
{
$this->_checkoutSession->setLastBillingAgreementId($agreement->getId());
}
}
// add recurring profiles information to the session
$profiles = $service->getRecurringPaymentProfiles();
if ($profiles)
{
$ids = array();
foreach ($profiles as $profile)
{
$ids[] = $profile->getId();
}
$this->_checkoutSession->setLastRecurringProfileIds($ids);
// TODO: send recurring profile emails
}
Mage::dispatchEvent(
'checkout_submit_all_after',
array('order' => $order, 'quote' => $this->getQuote(), 'recurring_profiles' => $profiles)
);
return $this;
}
How can I correct it ?