douzuan2814 2016-10-18 09:53
浏览 51
已采纳

Magento拆分订单通过产品的属性制造商

I am trying to split order by using product's manufacturer attribute.Like if there are 3 products in cart as 2 lee(manufacturer) products and 1 Levis(manufacturer) product.So when we place order it should generate 2 order one for lee and other one is for levis.

So with some help i am able to split order(problem in total but's it's ok)but it's creating order per product like 5 product 5 orders, so i am just asking is it possible to do as i want?

Here is code for split order which i am using currently

class PMTECH_Splitorder_Model_Checkout_Type_Onepage extends Mage_Checkout_Model_Type_Onepage
{
    /**
        * Create order based on checkout type. Create customer if necessary.
        *
        * @return Mage_Checkout_Model_Type_Onepage
    */
    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) 
        {

            $key= $key+1;
            $temparray[$key]['product_id']=  $item->getProduct()->getId();

            $temparray[$key]['qty']= $item->getQty();
            $cart->removeItem($item->getId());
            $cart->setSubtotal(0);
            $cart->setBaseSubtotal(0);

            $cart->setSubtotalWithDiscount(0);
            $cart->setBaseSubtotalWithDiscount(0);

            $cart->setGrandTotal(0);
            $cart->setBaseGrandTotal(0);

            $cart->setTotalsCollectedFlag(false);
            $cart->collectTotals();
        }
        $cart->save();

        foreach ($temparray as $key => $item) 
        {
            $customer_id = Mage::getSingleton('customer/session')->getId();

            $store_id = Mage::app()->getStore()->getId();
            $customerObj = Mage::getModel('customer/customer')->load($customer_id);
            $quoteObj = $cart;
            $storeObj = $quoteObj->getStore()->load($store_id);
            $quoteObj->setStore($storeObj);
            $productModel = Mage::getModel('catalog/product');
            $productObj = $productModel->load($item['product_id']);
            $quoteItem = Mage::getModel('sales/quote_item')->setProduct($productObj);
            $quoteItem->setBasePrice($productObj->getFinalPrice());
            $quoteItem->setPriceInclTax($productObj->getFinalPrice());
            $quoteItem->setData('original_price', $productObj->getPrice());
            $quoteItem->setData('price', $productObj->getPrice());
            $quoteItem->setRowTotal($productObj->getFinalPrice());
            $quoteItem->setQuote($quoteObj);
            $quoteItem->setQty($item['qty']);
            $quoteItem->setStoreId($store_id);
            $quoteObj->addItem($quoteItem);

            $quoteObj->setBaseSubtotal($productObj->getFinalPrice());
            $quoteObj->setSubtotal($productObj->getFinalPrice());
            $quoteObj->setBaseGrandTotal($productObj->getFinalPrice());
            $quoteObj->setGrandTotal($productObj->getFinalPrice());

            $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(false);
                $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;
    }

}

And i think it will be helpful to insert data in flat order quote item so i have that field in this table with product's manufacturer id inserted in it

Thank you in advance for any help.

  • 写回答

1条回答 默认 最新

  • dsi36131 2016-10-19 06:03
    关注

    I found solution on my own, posting answer so may be it can help someone.

    i am posting what i made changes in code.

    foreach ($cart->getAllItems() as $item) 
            {
    
                $key= $key+1;
                $product = Mage::getModel('catalog/product')->load($item->getProduct()->getId());
    
                $temparray[$product->getManufacturer()][] = array('product_id'=>$item->getProduct()->getId(),'qty'=>$item->getQty());
                //added this above line for grouping products by brands
                $cart->removeItem($item->getId());
                $cart->setSubtotal(0);
                $cart->setBaseSubtotal(0);
    
                $cart->setSubtotalWithDiscount(0);
                $cart->setBaseSubtotalWithDiscount(0);
    
                $cart->setGrandTotal(0);
                $cart->setBaseGrandTotal(0);
    
                $cart->setTotalsCollectedFlag(false);
                $cart->collectTotals();
            }
            $cart->save();
    
            foreach ($temparray as $key => $items) 
            { // this ends as it is in my question code
                foreach($items as $item){ //added this foreach
                    $customer_id = Mage::getSingleton('customer/session')->getId();
    
                    $store_id = Mage::app()->getStore()->getId();
                    $customerObj = Mage::getModel('customer/customer')->load($customer_id);
                    $quoteObj = $cart;
                    $storeObj = $quoteObj->getStore()->load($store_id);
                    $quoteObj->setStore($storeObj);
                    $productModel = Mage::getModel('catalog/product');
                    $productObj = $productModel->load($item['product_id']);
                    $quoteItem = Mage::getModel('sales/quote_item')->setProduct($productObj);
                    $quoteItem->setBasePrice($productObj->getFinalPrice());
                    $quoteItem->setPriceInclTax($productObj->getFinalPrice());
                    $quoteItem->setData('original_price', $productObj->getPrice());
                    $quoteItem->setData('price', $productObj->getPrice());
                    $quoteItem->setRowTotal($productObj->getFinalPrice() * $item['qty']);
                    $quoteItem->setQuote($quoteObj);
                    $quoteItem->setQty($item['qty']);
                    $quoteItem->setManufacturerName($key);//not sure about this that i need to add this or not
                    $quoteItem->setStoreId($store_id);
                    $quoteObj->addItem($quoteItem);
    
                    $quoteObj->setBaseSubtotal($productObj->getFinalPrice());
                    $quoteObj->setSubtotal($productObj->getFinalPrice());
                    $quoteObj->setBaseGrandTotal($productObj->getFinalPrice());
                    $quoteObj->setGrandTotal($productObj->getFinalPrice());
    
                    $quoteObj->setStoreId($store_id);
                    $quoteObj->collectTotals();
                    $quoteObj->save();
                    $this->_quote=$quoteObj;
                }
    

    Okay so by doing this now i am able to split order by brand as i want.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?