通过产品属性制造商

时间:2016-10-18 09:53:52

标签: php magento magento-1.9

我试图通过使用产品的制造商属性来拆分订单。如果购物车中有3个产品作为2个lee(制造商)产品和1个Levis(制造商)产品,那么当我们下订单时它应该生成2个订单一个李和另一个是levis。

所以在一些帮助下,我能够分割订单(总共有问题,但没关系),但它正在为每个产品创建订单,例如5个产品5个订单,所以我只是想问是否可以按我的意愿去做?

以下是我目前正在使用的拆分订单的代码

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;
    }

}

我认为在flat order quote item中插入数据会很有帮助,所以我在此表中有该字段,其中插入了产品的制造商ID

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

我自己找到了解决方案,发布了答案,所以可以帮助别人。

我发布了我在代码中所做的更改。

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;
            }

好的,通过这样做我现在能够按照我想要的品牌分割。