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

时间:2013-06-06 02:01:37

标签: magento magento-1.7

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

应用程序/代码/本地/永久/ 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

4 个答案:

答案 0 :(得分: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();
    }
}

答案 1 :(得分:1)

发现它!在购物车保存后将它放在任何地方。它将重置您的购物车并重新加载您的报价以重新计算所有产品的价格。

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

答案 2 :(得分: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();

答案 3 :(得分:-2)

最近,我们为Magento推出了免费的一流扩展 - 添加多个产品,以改善客户的在线购物体验。

此扩展程序允许用户添加多个产品,并通过单击将所有产品添加到购物车。更重要的是,它将允许客户从类别页面本身输入数量。

http://www.magentocommerce.com/magento-connect/add-multiple-products-to-cart.html

这正是您所需要的。