2

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 ?

1 Answer 1

1

You can try the following approach:

First override the saveOrder of Mage_Checkout_Model_Type_Onepage then put the code as given below:

public function saveOrder(){
    $quote = $this->getQuote();

    // First build an array with the items split by seller
    $sortedItems = array();
    foreach ($quote->getAllItems() as $item) {
          $seller= Mage::getModel('catalog/product')->load($item->getProductId())->getData('sold_by');
            $sortedItems[$seller][] = $item;
    }
    $allOrderId='';
    foreach ($sortedItems as $seller => $items) {
        foreach ($quote->getAllItems() as $item) {
            $quote->getItemsCollection()->removeItemByKey($item->getId());
        }

        foreach ($items as $item) {
            $quote->getItemsCollection()->addItem($item);
        }
        // Update totals for seller
        $cart = Mage::getSingleton('checkout/session')->getQuote();
        foreach($cart->getAllAddresses() as $address){
            $address->unsetData('cached_items_nonnominal');
            $address->unsetData('cached_items_nominal');
        }
        $address = $quote->getShippingAddress();
        $zipcode = $address->getPostcode();
        $country = $address->getCountry();
        // Update the cart's quote.
        $ShipCart = Mage::getSingleton('checkout/cart');
        $shipAddress = $ShipCart->getQuote()->getShippingAddress();
        $shipAddress->setCountryId($country)->setPostcode($zipcode)->setCollectShippingrates(true);
        $ShipCart->save();
        $cart->setTotalsCollectedFlag(false);
        $cart->collectTotals();//echo 1 ;die;
        // Delegate to parent method to place an order for each seller
        parent::saveOrder();
        $lastOrderId = Mage::getSingleton('checkout/session')
               ->getLastRealOrderId();
        if($allOrderId==''){
            $allOrderId=$allOrderId.$lastOrderId;
          }else{
            $allOrderId=$allOrderId.', '.$lastOrderId;   
        }
    }
     Mage::getSingleton('core/session')->setCurrentAllOrderId($allOrderId); 
    return $this;
}
8
  • Not working. I am not able to place the order now. Feb 16, 2017 at 8:41
  • is any error coming? Feb 16, 2017 at 9:16
  • No.. but when I press "Place Order" in the frontend, there are no changes made. The page remains as it is. @PrasantaHatui Feb 16, 2017 at 10:05
  • Check network from browser what response coming after "Place Order" button clicked. Feb 16, 2017 at 10:09
  • Not getting any response after "Place Order" button is . @PrasantaHatui Feb 16, 2017 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.