addProduct在magento中不起作用

时间:2013-05-28 10:16:47

标签: magento

在我的magento商店中,我添加了一些自定义模块以跳过结帐流程。曾几何时,它工作得很好。现在它似乎得到了一个错误。该产品未在报价中添加。

这是我的代码

public function orderAction()
     {   

            if(!Mage::getSingleton('customer/session')->isLoggedIn()) 
            {  
                $this->_redirectUrl(Mage::getBaseUrl().'customer/account/login');
                return;
            }

            $qty = 1;

            $customer = Mage::getSingleton('customer/session')->getCustomer();
            $email = $customer->getEmail();         

            require_once 'app/Mage.php';

            Mage::app('default');

            $store = Mage::app()->getStore('default');

            $customerGet = Mage::getModel('customer/customer');
            $customerGet->setStore($store);
            $customerGet->loadByEmail($email);

            $quote = Mage::getModel('sales/quote');
            $quote->setStore($store);
            $quote->assignCustomer($customerGet);

            $option_id1 = "My License";
            $option_value1 = "1 License";                   

            $product1 = Mage::getModel('catalog/product')->load(2);

            $buyInfo1 = array('qty' => $qty, 'options' => array( $option_id1 => $option_value1));

            $quote->addProduct($product1, $qty);        

            $billingAddress = $quote->getBillingAddress()->addData($customer->getPrimaryBillingAddress());

            $shippingAddress = $quote->getShippingAddress()->addData($customer->getPrimaryShippingAddress());

            $shippingAddress->setCollectShippingRates(true)->collectShippingRates()
                            ->setShippingMethod('freeshipping_freeshipping')
                            ->setPaymentMethod('free');

            $quote->getPayment()->importData(array('method' => 'free'));
            $quote->collectTotals()->save();

            $service = Mage::getModel('sales/service_quote', $quote);
            $service->submitAll();          

            $order = $service->getOrder();  

            $this->_mailOrder();

            $cartHelper = Mage::helper('checkout/cart');
            //Get all items from cart
            $items = $cartHelper->getCart()->getItems();
            //Loop through all of cart items                        
            foreach ($items as $item) {
                $itemId = $item->getItemId();
                //Remove items, one by one
                $cartHelper->getCart()->removeItem($itemId)->save();
            }                       

            $this->_redirect('downloadable/customer/products/');

     }

     private function _mailOrder()
     {   

          $orders = Mage::getModel('sales/order')->getCollection();
          $orders = $orders->getLastItem()->getIncrementId();

          $ip_address = $_SERVER['REMOTE_ADDR'];

          $ip_address_set = Mage::getModel('sales/order')->loadByIncrementId($orders)
                    ->setRemoteIp($ip_address)
                    ->setCustomerNoteNotify('1')
                    ->setCustomerIsGuest('0')
                    ->save();

          $order_mail = new Mage_Sales_Model_Order();
          $incrementId = $orders;
          $order_mail->loadByIncrementId($incrementId);

          try
          {
            $order_mail->sendNewOrderEmail();
          } 

          catch (Exception $ex)
          {
          }               


     }

请告诉我你的建议。

由于

2 个答案:

答案 0 :(得分:0)

您是否尝试在addProduct调用中保存报价(非常确定这是从1.4开始需要但可能是错误的)?

$quote->addProduct($product1, $qty)->save();

另一种方法是实例化购物车,并使用它通过以下方式添加到购物车:

$cart = Mage::getSingleton('checkout/cart');
$cart->addProduct($product, $qty);
$cart->save();

让我知道你是怎么过的!

答案 1 :(得分:0)

试试这个

$cart = Mage::getSingleton('checkout/cart');
$cart->init();
$cart->addProduct($product_id,$qty);
$quote = $cart->getQuote();
$shippingAddress = $quote->getShippingAddress();
$shippingAddress->setShippingMethod('flatrate_flatrate')->save();

// update session
$session->setCartWasUpdated(true);

// save the cart
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

根据您的要求编辑代码。

由于