在magento 1.7.0.2中将多个产品添加到购物车

在magento 1.7.0.2中将多个产品添加到购物车,magento,magento-1.7,Magento,Magento 1.7,我在magento 1.7.0.2中添加了新模块。我已经根据文章完成了。 当我同时添加了三个产品,但只有一个产品可以显示正确的价格,其他产品是$0.00,Subiotal是错误的。然后我点击“我的购物车”页面顶部,在侧边栏上的“我的购物车”中显示正确的价格 app/code/local/permitive/MultiAdd/controllers/Checkout/CartController.php require_once('Mage/Checkout/controllers/CartCon

我在magento 1.7.0.2中添加了新模块。我已经根据文章完成了。 当我同时添加了三个产品,但只有一个产品可以显示正确的价格,其他产品是$0.00,Subiotal是错误的。然后我点击“我的购物车”页面顶部,在侧边栏上的“我的购物车”中显示正确的价格

app/code/local/permitive/MultiAdd/controllers/Checkout/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();
    }
}
require_once('Mage/Checkout/controllers/CartController.php');
类永久\u多重添加\u签出\u CartController扩展了Mage\u签出\u CartController
{
/**
*向购物车操作添加多个产品
*基于Mage_Checkout_CartController::addAction()
*另见http://www.magentocommerce.com/boards/viewthread/8610/
*及http://www.magentocommerce.com/wiki/how_to_overload_a_controller
*/
公共函数addmultipleAction()
{
$productIds=$this->getRequest()->getParam('products');
如果(!is_数组($productId)){
$this->_goBack();
返回;
}
foreach($productId为$productId){
试一试{
$qty=$this->getRequest()->getParam('qty'.$productId,0);
如果($qty_getCart();
$cart->init();
//$cart=Mage::getModel('checkout/cart')->init();
$product=Mage::getModel('目录/产品')
->setStoreId(Mage::app()->getStore()->getId())
->加载($productId)
->setConfiguredAttributes($this->getRequest()->getParam('super_属性'))
->setGroupedProducts($this->getRequest()->getParam('super_group',array());
$eventArgs=array(
“产品”=>$product,
“数量”=>$qty,
'request'=>this->getRequest(),
'response'=>this->getResponse(),
);
Mage::dispatchEvent('checkout\u cart\u before\u add',$eventArgs);
$cart->addProduct($product,$qty);
Mage::dispatchEvent('checkout\u cart\u after\u add',$eventArgs);
$cart->save();
Mage::dispatchEvent('checkout\u cart\u add\u product',数组('product'=>$product));
$message=$this->(已成功将“%s”添加到您的购物车中,$product->getName());
Mage::getSingleton('checkout/session')->addSuccess($message);
}
捕获(法师核心例外$e){
if(Mage::getSingleton('checkout/session')->getUseNotice(true)){
Mage::getSingleton('checkout/session')->addNotice($product->getName()。:'。$e->getMessage());
}
否则{
Mage::getSingleton('checkout/session')->addError($product->getName()。:'。$e->getMessage());
}
}
捕获(例外$e){
Mage::getSingleton('checkout/session')->addException($e,$this->uuuu('cannotadditestoshoppingcart'));
}
}
$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>
...
。。。
/多重添加/签出\购物车/多重添加/
永久\u多重添加\u辅助对象
标准
永久性多重加法
多重加法
app\code\local\performal\MultiAdd\Helper\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);
    }
}
?>

app\etc\modules\app\u 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>

真的
地方的
0.1.0
如何在侧边栏的“我的购物车”中修正错误的价格? 对不起,我的英语不是我的母语


$cart=$this->\u 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();
    }
}
require_once('Mage/Checkout/controllers/CartController.php');
类永久\u多重添加\u签出\u CartController扩展了Mage\u签出\u CartController
{
/**
*向购物车操作添加多个产品
*基于Mage_Checkout_CartController::addAction()
*另见http://www.magentocommerce.com/boards/viewthread/8610/
*及http://www.magentocommerce.com/wiki/how_to_overload_a_controller
*/
公共函数addmultipleAction()
{
$productIds=$this->getRequest()->getParam('products');
如果(!is_数组($productId)){
$this->_goBack();
返回;
}
$cart=$this->_getCart();
foreach($productId为$productId){
试一试{
$qty=$this->getRequest()->getParam('qty'.$productId,0);
如果($qty_getCart();
$product=Mage::getModel('目录/产品')
->setStoreId(Mage::app()->getStore()->getId())
->加载($productId)
->setConfiguredAttributes($this->getRequest()->getParam('super_属性'))
->setGroupedProducts($this->getRequest()->getParam('super_group',array());
$eventArgs=array(
“产品”=>$product,
“数量”=>$qty,
'request'=>this->getRequest(),
'response'=>this->getResponse(),
);
Mage::dispatchEvent('checkout\u cart\u before\u add',$eventArgs);
//$cart=Mage::getModel('checkout/cart')->init();
$cart->addProduct($product,$qty);
Mage::dispatchEvent('checkout\u cart\u after\u add',$eventArgs);
//$cart->save();
Mage::dispatchEvent('checkout\u cart\u add\u product',数组('product'=>$product));
$message=$this->(已成功将“%s”添加到您的购物车中,$product->getName());
Mage::getSingleton('签出/会话')
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();
    }
}
Mage::unregister('_singleton/checkout/cart');
Mage::unregister('_singleton/checkout/session');
$cart = $this->_getCart();
$this->_getQuote();
if ($cart->getQuote()->getItemsCount()) {
    $cart->init();
    $cart->save();
}
 $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();