Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 1 of 9
Add multiple products to cart - example
 
uni-man
Member
 
Total Posts:  34
Joined:  2008-04-04
 

Hi all

On a site I’m building, customers want to add many grocery items from a category into their cart with as few clicks as possible. For example the customer might want 10 carrots, 2 broccoli, 1kg of onions, 2k of potatoes (which are all on the same page / category).

Heres how:
Create a new helper (note: this is a hacked version of Mage/Checkout/helper/Cart.php)

class Company_Module_Helper_Multiple 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);
    
}
}

Override template/catalog/product/list.phtml in your theme.
Just below <?php echo $this->getToolbarHtml() ?> at about line 35, add

<form action="<?php echo $this->helper( 'Module/multiple' )->getAddToCartUrl() ?>" method="post" id="product_addtocart_form">
<
button class="form-button" onclick="productAddToCartForm.submit()"><span><?php echo $this->__('Add Items to Cart'?></span></button>

Then just below <?php if($_product->isSaleable()): ?> add this, which replaces the Add to Cart button / link

<fieldset class="add-to-cart-box">
                        <
input type="hidden" name="products[]" value="<?php echo $_product->getId() ?>" />
                        <
legend><?php echo $this->__('Add Items to Cart'?></legend>
                        <
span class="qty-box"><label for="qty<?php echo $_product->getId() ?>"><?php echo $this->__('Qty'?>:</label>
                        <
input name="qty<?php echo $_product->getId() ?>" type="text" class="input-text qty" id="qty<?php echo $_product->getId() ?>" maxlength="12" value="" /></span>
                    </
fieldset>

then close the form just above
<?php echo $this->getToolbarHtml() ?>

<button class="form-button" onclick="productAddToCartForm.submit()"><span><?php echo $this->__('Add Items to Cart'?></span></button>
</
form>

Create a new file Company/Module/controllers/Checkout/CartController.php

require_once 'Mage/Checkout/controllers/CartController.php';

class 
Company_Module_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' $productId0);
                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,
                    
'additional_ids' => array(),
                    
'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();
    
}
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
uni-man
Member
 
Total Posts:  34
Joined:  2008-04-04
 

Then do the usual stuff with Company/Module/etc/config.xml

<?xml version="1.0"?>
<con
    
<modules>
        <
Company_Module>
            <
version>0.1.0</version>
        </
Company_Module>
    </
modules>
    <global>
        <
rewrite>
            <
company_module_checkout_cart>
                <
from><![CDATA[#^/checkout/cart/addmultiple/.*$#]]></from>
                
<to>/module/checkout_cart/addmultiple/</to>
            </
company_module_checkout_cart
        </
rewrite>
        <
helpers>
            <
Module>
                <class>
Company_Module_Helper</class>
            </
Module>
        </
helpers>
    </global>
    <
frontend>
        <
routers>
            <
company_module>
                <use>
standard</use>
                <
args>
                    <
module>Company_Module</module>
                    <
frontName>module</frontName>
                </
args>
            </
company_module>
        </
routers>
    </
frontend>
</
config>

It took me ages to get this going because I forgot to add a .* on to the end of the regular expression in config.xml rewrite/from. I couldn’t figure out why I could override existing methods but couldn’t create new ones…

If there is not enough stock for a product, then an error message is displayed but the rest of the products are added. This has only been tested on simple products.

 
Magento Community Magento Community
Magento Community
Magento Community
 
dtanovic
Jr. Member
 
Total Posts:  6
Joined:  2008-06-19
 

This sounds exactly like what I would like to have eventually.  Unfortunately I know almost nothing about coding so I’ll need to get some outside help. 

Is this a relatively simple change?

Does this affect the core or is it affected by magento updates?

dt

 
Magento Community Magento Community
Magento Community
Magento Community
 
uni-man
Member
 
Total Posts:  34
Joined:  2008-04-04
 

No, it doesn’t affect the core.
Yes, its a “simple” change relative to doing other stuff in Magento, but it took me ages to figure out how to do it, which is why i posted the solution.
If you can find someone who knows how to tweak magento (maybe advertise on the job board) then they should be able to implement this in a short time.

hope this helps

 
Magento Community Magento Community
Magento Community
Magento Community
 
dtanovic
Jr. Member
 
Total Posts:  6
Joined:  2008-06-19
 

Thanks uni-man.  I’ll get this all figured out one day.  The good news my old online store is functioning until I get this up and going.

 
Magento Community Magento Community
Magento Community
Magento Community
 
dtanovic
Jr. Member
 
Total Posts:  6
Joined:  2008-06-19
 

I posted it on getafreelancer.com

 
Magento Community Magento Community
Magento Community
Magento Community
 
davy
Jr. Member
 
Total Posts:  20
Joined:  2008-01-21
 

Thanks I got this working on Magento 1.0.

Updating to 1.1 however breaks the quantity for a yet unknown reason. It looks like only the left most integer of any amount is put into the cart e.g 62 -> 6, 100->1, 5->5

Any idea where the problem might be?

Thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
weckl
Member
 
Total Posts:  50
Joined:  2008-07-13
Singapore
 

Hello Uni-man,

Do you have a live example of how it works?  I’m actually looking for a quickorder feature which has been mentioned in this forum several times..

a real good example of a quickorder feature is one that’s on schwans.com website

http://www.schwans.com/catalog/catalog_order.jsp

 
Magento Community Magento Community
Magento Community
Magento Community
 
Andrewc
Jr. Member
 
Total Posts:  13
Joined:  2008-08-21
 

Just cant get this 2 work..

I am trying to implement this on the wishlist page. I know the wishlist page has a “add all to cart” functionality but I’m adding something else.

I have done the steps to what I understand, the wishlist page spits out strange characters:

Something like this:
‹������ÌZ{sÛ6ÿ»™éw@™ÎÙ™EÉyZ–tçØNã9;ÎÄªãHHD,�Zv3ýî·>D=(Kv“ÆIØÇ‹Åîà÷¾Ôýáðìàâ×÷G$4‘ ï~}r|@×ó>>=ð¼Ã‹CòËÛ‹ÓÒn¶È¹QÜ7žwôÎ!NhLÒñ¼ÉdÒœx7(¥lù£«-O30ÓuVËM$bÝ["¡½»»›1:HÔ4÷;¤|²B à?]Í`}rzK>r ® ézY#PFÌP‚z\öGʯ{΁Œ ‹{q›0‡øÙ[Ï1ìÆx¨wø!Uš™^jFî+‡x¥˜˜F¬ç

Why no error message?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Andrewc
Jr. Member
 
Total Posts:  13
Joined:  2008-08-21
 

Ok I did not tell Magento that I had a new module.

Found help here on how to do this: http://www.getfido.net/blog/magento/simple-custom-module.php

 
Magento Community Magento Community
Magento Community
Magento Community
 
Georg Ringer
Member
 
Avatar
Total Posts:  63
Joined:  2008-07-04
Linz, Austria
 
davy - 20 August 2008 01:44 PM

Thanks I got this working on Magento 1.0.

Updating to 1.1 however breaks the quantity for a yet unknown reason. It looks like only the left most integer of any amount is put into the cart e.g 62 -> 6, 100->1, 5->5

Any idea where the problem might be?

Thanks

hi, did you solve this?
thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
Georg Ringer
Member
 
Avatar
Total Posts:  63
Joined:  2008-07-04
Linz, Austria
 

as usual I find it after posting!
/app/code/core/Mage/Checkout/Model/Cart.php
line 208 change
$request = $this->_getProductRequest($info);
to
$request = (int) $info;

I will report that ASAP to the bugtracker: http://www.magentocommerce.com/bug-tracking/issue?issue=3611

 
Magento Community Magento Community
Magento Community
Magento Community
 
jpluttme
Member
 
Total Posts:  34
Joined:  2008-04-28
 

So I get the error:

error parsing attribute name in /home/youngag/public_html/magento/lib/Varien/Simplexml/Config.php on line 500

Anyone know what this could be from?

Solution:

<config> tag was not complete in above example code for Company/Module/etc/config.xml

So now that is fixed I get this error:

class Mage_Module_Helper_Multiple not found in app/code core/Mage/Core/Model/App line 785.

Any thoughts?

 
Magento Community Magento Community
Magento Community
Magento Community
 
jpluttme
Member
 
Total Posts:  34
Joined:  2008-04-28
 

This is what I have in the app/etc/modules directory.  Maybe I have it wrong.

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

Thanks.

 
Magento Community Magento Community
Magento Community
Magento Community
 
jpluttme
Member
 
Total Posts:  34
Joined:  2008-04-28
 

Anyone gotten this working in 1.1.6?  Please...anyone....

 
Magento Community Magento Community
Magento Community
Magento Community
 
zero cool
Jr. Member
 
Total Posts:  10
Joined:  2008-08-26
 

Hi jpluttme,

I’m coding this mod 4 u in SL, smile. Just replace the Company by Mage. When I do it, not fatal error anymore. But the URL I got is not exactly. I do not know how to fix it.

Thanks,

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 9