Scenario  :  adding mutiple products to cart in magento which have quantity greater than zero value.
Solution :
1/ Create a button that send a request for adding multiple products to cart in magento.
2/Create a controller to handle it.It will extend Mage_Checkout_CartController
Let’s begin coding :
1/Create a button that send a request for adding multiple products to cart in magento.
Go to the the file : app/design/frontend/default/your-current-theme/catalog/product/list.phtml
Please notify the highlight code and put them in proper section.
<?php
/**
* Product list template
*
* @see Mage_Catalog_Block_Product_List
*/
?>
<?php
$_productCollection=$this->getLoadedProductCollection();
$_helper = $this->helper('catalog/output');
?>
<?php if(!$_productCollection->count()): ?>
<p><?php echo $this->__('There are no products matching the selection.') ?></p>
<?php else: ?>
<div class="category-products">
<form action="<?php echo Mage::getUrl('checkout/cart/addmultiple') ;?>" method="post" id="products_addtocart_form" enctype="multipart/form-data">
<input type="hidden" name="multiple_products" id="multiple_products" value="" />
<button onclick="this.form.submit()"><span><span><?php echo $this->__('Put all in basket') ?></span></span></button>
</form>
<?php echo $this->getToolbarHtml() ?>
<?php // List mode ?>
<?php if($this->getMode()!='grid'): ?>
<?php $_iterator = 0; ?>
<ol id="products-list">
<?php foreach ($_productCollection as $_product): ?>
<li>
<?php // Product Image ?>
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a>
<?php // Product description ?>
<div>
<div>
<?php $_productNameStripped = $this->stripTags($_product->getName(), null, true); ?>
<h2><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped; ?>"><?php echo $_helper->productAttribute($_product, $_product->getName() , 'name'); ?></a></h2>
<?php if($_product->getRatingSummary()): ?>
<?php echo $this->getReviewsSummaryHtml($_product) ?>
<?php endif; ?>
<?php echo $this->getPriceHtml($_product, true) ?>
<?php if($_product->isSaleable()): ?>
<p><button type="button" title="<?php echo $this->__('Add to Cart') ?>" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button></p>
<input type="hidden" name="products[]" value="<?php echo $_product->getId() ?>" />
<?php else: ?>
<p><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
<div>
<?php echo $_helper->productAttribute($_product, $_product->getShortDescription(), 'short_description') ?>
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $_productNameStripped ?>"><?php echo $this->__('Learn More') ?></a>
</div>
<ul>
<?php if ($this->helper('wishlist')->isAllow()) : ?>
<li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>"><?php echo $this->__('Add to Wishlist') ?></a></li>
<?php endif; ?>
<?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
<li><span>|</span> <a href="<?php echo $_compareUrl ?>"><?php echo $this->__('Add to Compare') ?></a></li>
<?php endif; ?>
</ul>
</div>
</div>
</li>
<?php endforeach; ?>
</ol>
<script type="text/javascript">decorateList('products-list', 'none-recursive')</script>
<?php else: ?>
<?php // Grid Mode ?>
<?php $_collectionSize = $_productCollection->count() ?>
<?php $_columnCount = $this->getColumnCount(); ?>
<?php $i=0; foreach ($_productCollection as $_product): ?>
<?php if ($i++%$_columnCount==0): ?>
<ul>
<?php endif ?>
<li>
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a>
<h2><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>"><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></a></h2>
<?php if($_product->getRatingSummary()): ?>
<?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
<?php endif; ?>
<?php echo $this->getPriceHtml($_product, true) ?>
<div>
<?php if($_product->isSaleable()): ?>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
<input type="hidden" name="products[]" value="<?php echo $_product->getId() ?>" />
<?php else: ?>
<p><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
<ul>
<?php if ($this->helper('wishlist')->isAllow()) : ?>
<li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>"><?php echo $this->__('Add to Wishlist') ?></a></li>
<?php endif; ?>
<?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
<li><span>|</span> <a href="<?php echo $_compareUrl ?>"><?php echo $this->__('Add to Compare') ?></a></li>
<?php endif; ?>
</ul>
</div>
</li>
<?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
</ul>
<?php endif ?>
<?php endforeach ?>
<script type="text/javascript">decorateGeneric($$('ul.products-grid'), ['odd','even','first','last'])</script>
<?php endif; ?>
<div>
<?php echo $this->getToolbarHtml() ?>
</div>
</div>
<?php endif; ?>
<script type="text/javascript">
jQuery("#products_addtocart_form").on('submit',function(e){
var products = {};
jQuery('input[name="products[]"]').each(function() {
var productId = jQuery(this).val();
var productqty = jQuery('input[qtyproduct='+ productId +']').val();
if(parseInt(productqty)>0){
products[productId]= productqty;
}
});
jQuery('#multiple_products').val(JSON.stringify(products));
return;
});
</script>
2/Create a controller to handle it.It will extend Mage_Checkout_CartController
– Declare module : Create app/etc/modules/Yourcompany_Multiplecart.xml for registering new module in magento :
<?xml version="1.0"?> <config> <modules> <Yourcompany_Multiplecart> <active>true</active> <codePool>local</codePool> </Yourcompany_Multiplecart> </modules> </config>
Create app/code/local/Yourcompany/Multiplecart/etc/config.xml
<?xml version="1.0"?> <config> <modules> <Yourcompany_Multiplecart> <version>1.0.1</version> </Yourcompany_Multiplecart> </modules> <global> <helpers> <multiplecart> <class>Yourcompany_Multiplecart_Helper</class> </multiplecart> </helpers> </global> <frontend> <routers> <checkout> <args> <modules> <Yourcompany_Multiplecart before="Mage_Checkout">Yourcompany_Multiplecart_Checkout</Yourcompany_Multiplecart> </modules> </args> </checkout> </routers> </frontend> </config>
Create CartController.php : app/code/local/Yourcompany/Multiplecart/controllers/Checkout/CartController.php
<?php
require_once 'Mage/Checkout/controllers/CartController.php';
class Yourcompany_Multiplecart_Checkout_CartController extends Mage_Checkout_CartController
{
/**
* Adding multiple products to shopping cart action
* based on Mage_Checkout_CartController::addAction()
* catalog/product/list.phtml
*
*/
public function addmultipleAction()
{
$productIds = $this->getRequest()->getParam('multiple_products');
$productIds = json_decode($productIds,true);
if (!is_array($productIds)) {
$this->_goBack();
return;
}
foreach( $productIds as $prodId => $prodQty) {
try {
$prodId = intval($prodId);
$qty = intval($prodQty);
if($qty<=0)continue;
$cart = $this->_getCart();
$cart->init();
$product = Mage::getModel('catalog/product')
->setStoreId(Mage::app()->getStore()->getId())
->load($prodId);
//->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(),
);
if(!$product){
continue;
}
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();
}
}
Try to refresh your cache to see this changes. Happy coding!
Jutshu
April 4
That’s good tutorial. Keep it up guy!
Chi Duong
April 4
Thank for great infor!
Here I got another suggestion for users that are not familiar with coding process. This module works great on my site so far!
check it out: http://bsscommerce.com/magento-add-multiple-products-to-cart.html
Inderjit SIngh
April 4
I am getting 404 error for this