2013-06-06 130 views
2

我在Magento 1.7.0.2.I已根據文章做了添加新的模塊。 http://www.magentocommerce.com/boards/viewthread/9797/P30/。 當我同時添加三件商品時,只有一件商品可以顯示正確的價格,其他商品是0.00美元,Subiotal是錯誤的。然後我點擊「我的購物車」頁面的頂部,正確的價格在「我的購物車「在側邊欄上。多個產品添加到購物車在Magento 1.7.0.2

應用程序/代碼/本地/萬年曆/ MultiAdd /控制器/結帳/ CartController.php

require_once('Mage/Checkout/controllers/CartController.php'); 
class Perpetual_MultiAdd_Checkout_CartController extends Mage_Checkout_CartController 
{ 
    /** 
    * Adding multiple products to shopping cart action 
    * based on Mage_Checkout_CartController::addAction() 
    * see also http://www.magentocommerce.com/boards/viewthread/8610/ 
    * and http://www.magentocommerce.com/wiki/how_to_overload_a_controller 
    */ 
    public function addmultipleAction() 
    { 
     $productIds = $this->getRequest()->getParam('products'); 
     if (!is_array($productIds)) { 
      $this->_goBack(); 
      return; 
     } 
     foreach($productIds as $productId) { 
      try { 
       $qty = $this->getRequest()->getParam('qty' . $productId, 0); 
       if ($qty <= 0) continue; // nothing to add 
       $cart = $this->_getCart(); 
       $cart->init(); 
       //$cart = Mage::getModel('checkout/cart')->init(); 
       $product = Mage::getModel('catalog/product') 
        ->setStoreId(Mage::app()->getStore()->getId()) 
        ->load($productId) 
        ->setConfiguredAttributes($this->getRequest()->getParam('super_attribute')) 
        ->setGroupedProducts($this->getRequest()->getParam('super_group', array())); 
       $eventArgs = array(
        'product' => $product, 
        'qty' => $qty, 
        'request' => $this->getRequest(), 
        'response' => $this->getResponse(), 
       ); 
       Mage::dispatchEvent('checkout_cart_before_add', $eventArgs); 

       $cart->addProduct($product, $qty); 
       Mage::dispatchEvent('checkout_cart_after_add', $eventArgs); 
       $cart->save(); 
       Mage::dispatchEvent('checkout_cart_add_product', array('product'=>$product)); 
       $message = $this->__('%s was successfully added to your shopping cart.', $product->getName());  
       Mage::getSingleton('checkout/session')->addSuccess($message); 
      } 
      catch (Mage_Core_Exception $e) { 
       if (Mage::getSingleton('checkout/session')->getUseNotice(true)) { 
        Mage::getSingleton('checkout/session')->addNotice($product->getName() . ': ' . $e->getMessage()); 
       } 
       else { 
        Mage::getSingleton('checkout/session')->addError($product->getName() . ': ' . $e->getMessage()); 
       } 
      } 
      catch (Exception $e) { 
       Mage::getSingleton('checkout/session')->addException($e, $this->__('Can not add item to shopping cart')); 
      } 
     } 
     $this->_goBack(); 
    } 
} 

我的模板:

... 
<form action="<?php echo $this->helper('multiadd/cart')->getAddToCartUrl() ?>" method="post" id="productAddToCartForm"> 
    ... 
    <label for="qty<?php echo $_product->getId()?>"><?php echo $this->__('Qty') ?>:</label> 
    <input type="text" name="qty<?php echo $_product->getId()?>" id="qty<?php echo $_product->getId()?>" maxlength="12" value="<?php echo ($this->getMinimalQty($_product)?$this->getMinimalQty($_product):0) ?>" class="input-text qty" /> 
    ... 
    <button class="button btn-cart" type="button" onclick="productAddToCartForm.submit()"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button> 
</form> 
... 

應用程序\代碼\本地\萬年曆\ MultiAdd \等\ config.xml中

<?xml version="1.0"?> 
<config> 
    <modules> 
     <Perpetual_MultiAdd> 
      <version>0.1.0</version> 
     </Perpetual_MultiAdd> 
    </modules> 
    <global> 
     <rewrite> 
      <perpetual_multiadd_checkout_cart> 
       <from><![CDATA[#^/checkout/cart/addmultiple/.*$#]]></from> 
       <to>/multiadd/checkout_cart/addmultiple/</to> 
      </perpetual_multiadd_checkout_cart> 
     </rewrite> 
     <helpers> 
      <multiadd> 
       <class>Perpetual_MultiAdd_Helper</class> 
      </multiadd> 
     </helpers> 
    </global> 
    <frontend> 
     <routers> 
      <perpetual_multiadd> 
       <use>standard</use> 
       <args> 
        <module>Perpetual_MultiAdd</module> 
        <frontName>multiadd</frontName> 
       </args> 
      </perpetual_multiadd> 
     </routers> 
    </frontend> 
</config> 

應用程序\代碼\本地\萬年曆\ MultiAdd \輔助\ Cart.php

<?php 
class Perpetual_MultiAdd_Helper_Cart extends Mage_Core_Helper_Url 
{ 
    /** 
    * Return url to add multiple items to the cart 
    * @return url 
    */ 
    public function getAddToCartUrl() 
    { 
     if ($currentCategory = Mage::registry('current_category')) { 
      $continueShoppingUrl = $currentCategory->getUrl(); 
     } else { 
      $continueShoppingUrl = $this->_getUrl('*/*/*', array('_current'=>true)); 
     } 

     $params = array(
      Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED => Mage::helper('core')->urlEncode($continueShoppingUrl) 
     ); 

     if ($this->_getRequest()->getModuleName() == 'checkout' 
      && $this->_getRequest()->getControllerName() == 'cart') { 
      $params['in_cart'] = 1; 
     } 
     return $this->_getUrl('checkout/cart/addmultiple', $params); 
    } 
} 
?> 

應用程序\等\模塊\ Perpetual_MultiAdd.xml

<?xml version="1.0"?> 
<config> 
    <modules> 
     <Perpetual_MultiAdd> 
      <active>true</active> 
      <codePool>local</codePool> 
      <version>0.1.0</version> 
     </Perpetual_MultiAdd> 
    </modules> 
</config> 

如何修復錯誤的價格在側邊欄上的 「我的車」? 我很抱歉我的英語,這不是我的母語。

enter image description here

回答

3

$cart = $this->_getCart();$cart->save();外的foreach。

require_once('Mage/Checkout/controllers/CartController.php'); 
class Perpetual_MultiAdd_Checkout_CartController extends Mage_Checkout_CartController 
{ 
    /** 
    * Adding multiple products to shopping cart action 
    * based on Mage_Checkout_CartController::addAction() 
    * see also http://www.magentocommerce.com/boards/viewthread/8610/ 
    * and http://www.magentocommerce.com/wiki/how_to_overload_a_controller 
    */ 
    public function addmultipleAction() 
    { 
     $productIds = $this->getRequest()->getParam('products'); 
     if (!is_array($productIds)) { 
      $this->_goBack(); 
      return; 
     } 


     $cart = $this->_getCart(); 

     foreach($productIds as $productId) { 

      try { 
       $qty = $this->getRequest()->getParam('qty' . $productId, 0); 
       if ($qty <= 0) continue; // nothing to add 
       //$cart = $this->_getCart(); 
       $product = Mage::getModel('catalog/product') 
        ->setStoreId(Mage::app()->getStore()->getId()) 
        ->load($productId) 
        ->setConfiguredAttributes($this->getRequest()->getParam('super_attribute')) 
        ->setGroupedProducts($this->getRequest()->getParam('super_group', array())); 
       $eventArgs = array(
        'product' => $product, 
        'qty' => $qty, 
        'request' => $this->getRequest(), 
        'response' => $this->getResponse(), 
       ); 
       Mage::dispatchEvent('checkout_cart_before_add', $eventArgs); 

       //$cart = Mage::getModel('checkout/cart')->init(); 

       $cart->addProduct($product, $qty); 
       Mage::dispatchEvent('checkout_cart_after_add', $eventArgs); 
       //$cart->save(); 
       Mage::dispatchEvent('checkout_cart_add_product', array('product'=>$product)); 
       $message = $this->__('%s was successfully added to your shopping cart.', $product->getName());  
       Mage::getSingleton('checkout/session')->addSuccess($message); 
      } 
      catch (Mage_Core_Exception $e) { 
       if (Mage::getSingleton('checkout/session')->getUseNotice(true)) { 
        Mage::getSingleton('checkout/session')->addNotice($product->getName() . ': ' . $e->getMessage()); 
       } 
       else { 
        Mage::getSingleton('checkout/session')->addError($product->getName() . ': ' . $e->getMessage()); 
       } 
      } 
      catch (Exception $e) { 
       Mage::getSingleton('checkout/session')->addException($e, $this->__('Can not add item to shopping cart')); 
      } 

     } 
     $cart->save(); 

     $this->_goBack(); 
    } 
} 
+0

如何實現magento2一樣嗎? –

1

找到了!在購物車保存後將其放在任何位置。它會重置您的購物車並重新載入您的報價以重新計算所有產品的價格。

Mage::unregister('_singleton/checkout/cart'); 
Mage::unregister('_singleton/checkout/session'); 
$cart = $this->_getCart(); 
$this->_getQuote(); 
if ($cart->getQuote()->getItemsCount()) { 
    $cart->init(); 
    $cart->save(); 
} 
0

可能是這個人會有所幫助:

http://deepakbhatta.com/magento-add-multiple-items-to-cart/

概述是:

$cart = Mage::helper('checkout/cart')->getCart(); 
     $ms=""; 
     foreach($validProducts as $sku => $qty) { 
      $params = array('qty' => $qty); 
      $id = Mage::getModel('catalog/product')->getIdBySku($sku); 
      $product = Mage::getModel('catalog/product')->load($id);; 
      $cart->addProduct($product, $params); 
      $msg .= $product->getName(). " is successfully added into cart<br>"; 
     } 
     $cart->save(); 
相關問題