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

标签 magento magento-1.7

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

app/code/local/Perpetual/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();
    }
}

我的模板:

...
<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>
...

app\code\local\Perpetual\MultiAdd\etc\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> 

app\code\local\Perpetual\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);
    }
}
?>

应用程序\etc\modules\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

最佳答案

$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();
    }
}

关于magento - 在magento 1.7.0.2中将多个产品添加到购物车,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16952580/

相关文章:

sql - Magento - 在数据库中更改外观和包

大型 Magento 的 MySql 配置

caching - Magento 将当前产品 ID 传递给模块

php - Magento:对产品集合进行排序

php - 通过产品 ID 创建指向 Magento 产品的直接链接

Magento 模块 SQL 不运行

magento - 在magento中添加一个新的属性来订购

Magento CE 1.7.0.2 – 导入的产品不显示在前端

php - Magento Rest api 通过电子邮件 ID 获取客户

magento - 如何在magento中创建依赖属性