Here, I am going to explain how to add multiple products into cart simultaneously.
Step 1: Create namespace
First thing’s first, we have to create our namespace XML. In app/etc/modules, create a file called Namespace_AddMultipleProducts.xml, and put this inside of it:
<?xml version=”1.0″?>
<config>
<modules>
<Namespace_AddMultipleProducts>
<active>true</active>
<codePool>local</codePool>
</Namespace_AddMultipleProducts>
</modules>
</config>
Step 2: Create directory structure
Now we create the directories where our code will sit. Under app/code/local, create this directory structure:
app/code/local/Namespace
app/code/local/Namespace/AddMultipleProducts
app/code/local/Namespace/AddMultipleProducts/controllers
app/code/local/Namespace/AddMultipleProducts/etc
Step 3: Create our module’s configuration
Step 1 we told Magento that we made a module, and where it is. Now we have to tell it what to do. In app/code/local/Namespace/AddMultipleProducts/etc, create a file called config.xml, and put this inside of it:
<?xml version=”1.0″ encoding=”UTF-8″?>
<config>
<modules>
<Namespace_AddMultipleProducts>
<version>1.0</version>
</Namespace_AddMultipleProducts>
</modules>
<frontend>
<routers>
<addmultipleproducts>
<use>standard</use>
<args>
<module>Namespace_AddMultipleProducts</module>
<frontName>multiadd</frontName>
</args>
</addmultipleproducts>
</routers>
</frontend>
</config>
Step 4: Create the controller that will handle our request
Finally, we have to put the PHP code in a controller which contains the logic of systematically adding multiple products to the cart at the same time. In app/code/local/Namespace/AddMultipleProducts/controllers, create a file called AddController.php (watch the CaSe here!), and put this inside of it:
$<?php
class Namespace_AddMultipleProducts_AddController extends Mage_Core_Controller_Front_Action {
public function indexAction() {
$products = $this->getRequest()->getParam(‘products’);
$cart = Mage::getModel(‘checkout/cart’);
$cart->init();
/* @var $pModel Mage_Catalog_Model_Product */
foreach ($products as $product_id) {
if ($product_id == ”) {
continue;
}
$pModel = Mage::getModel(‘catalog/product’)->load($product_id);
if ($pModel->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_SIMPLE) {
try {
$cart->addProduct($pModel, array(‘qty’ => ‘1’));
}
catch (Exception $e) {
continue;
}
}
}
$cart->save();
if ($this->getRequest()->isXmlHttpRequest()) {
exit(‘1’);
}
$this->_redirect(‘checkout/cart’);
}
}
Step 5: Add checkbox and submit button in list file
In app/design/frontend/[your_package]/[your_theme]/template/catalog/product/list.phtml, add form tag at starting of file and close form tag at end of file.
For example,
<form name=”multiadd” action=”<?php echo Mage::getBaseUrl().’multiadd/add/’?>”>
Your List File Code….
</form>
Now, add checkbox just after add to cart button in list as well as grid mode in List file.
<input type=”checkbox” value=”<?php echo $_product->getId()?>” name=”products[]” id=”products-<?php echo $_product->getId()?>” onclick=”SetButtonStatus(‘products-<?php echo $_product->getId()?>’)”/>
Now, add submit button just before close form tag </form> in List file..
<button type=”submit” title=”<?php echo $this->__(‘Multiple Add to Cart’) ?>” id=”btnButton” disabled=”disabled”><span><span><?php echo $this->__(‘Multiple Add to Cart’) ?></span></span></button>
At last, add following code at bottom of List file.
<script type=”text/javascript”>
function SetButtonStatus(sender)
{
var list, index, item, checkedCount;
checkedCount = 0;
list = document.getElementsByTagName(‘input’);
for (index = 0; index < list.length; ++index)
{
item = list[index];
if (item.getAttribute(‘type’) === “checkbox”
&& item.checked
&& item.name === “products[]”)
{
++checkedCount;
}
}
if(document.getElementById(sender).checked == 1)
{
document.getElementById(‘btnButton’).removeAttribute(‘disabled’);
}
else
{
if(checkedCount > 0)
{
document.getElementById(‘btnButton’).removeAttribute(‘disabled’);
}
else
{
document.getElementById(‘btnButton’).setAttribute(‘disabled’, ‘disabled’);
}
}
}
</script>
Note: This will only work with Simple Products, as there is no real way of knowing what the options are for other products type.
Thats it!!!