2013-05-28 41 views
0

在我的magento商店中,我添加了一些自定義模塊以跳過結帳過程。曾幾何時,它工作得很好。現在它似乎得到一個錯誤。該產品不在報價中添加。addProduct不工作在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) 
      { 
      }    


    } 

請給我你的建議。

感謝

+0

請問能否介紹一下添加產品時引用的錯誤? – Zaheerabbas

回答

0

你試過節能報價在addProduct命令調用(很肯定,因爲1.4本是必要的,但可能是錯誤的)?

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

另一種方式是實例化的車,並用它來添加到購物車通過:

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

讓我知道你上車!

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

編輯代碼,按您的要求。

謝謝