Adding multiple products to the cart simultaneously in Magento
by Dan on Sep.09, 2010, under Magento, PHP
Some people ask me if it’s possible to do what some other sites do, allowing you to check a box next to a product on a listing page, and then add all selected items to the cart in one go. This is possible in Magento, it’s just not built in. There’s a few ways you can do this. You can either make an ajax call using a button against each product, which fires off a GET request to the server to add. This method will most likely return the HTML for the cart, and since we’re not interested in the response, said HTML can be discarded.
Alternatively, you can write a small custom controller to do mass-add based on a GET or POST request (if you still wish to maintain the checkbox functionality). Here’s how I did it:
Step 1: Create namespace
First thing’s first, we have to create our namespace XML. In app/etc/modules, create a file called BTS_AddMultipleProducts.xml, and put this inside of it:
<?xml version="1.0" encoding="UTF-8"?> <config> <modules> <BTS_AddMultipleProducts> <active>true</active> <codePool>local</codePool> </BTS_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/BTS
- app/code/local/BTS/AddMultipleProducts
- app/code/local/BTS/AddMultipleProducts/controllers
- app/code/local/BTS/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/BTS/AddMultipleProducts/etc, create a file called config.xml, and put this inside of it:
<?xml version="1.0" encoding="UTF-8"?> <config> <modules> <BTS_AddMultipleProducts> <version>1.0</version> </BTS_AddMultipleProducts> </modules> <frontend> <routers> <addmultipleproducts> <use>standard</use> <args> <module>BTS_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/BTS/AddMultipleProducts/controllers, create a file called AddController.php (watch the CaSe here!), and put this inside of it:
class BTS_AddMultipleProducts_AddController extends Mage_Core_Controller_Front_Action { public function indexAction() { $products = explode(',', $this->getRequest()->getParam('products')); $cart = Mage::getModel('checkout/cart'); $cart->init(); $pModel = Mage::getSingleton('catalog/product'); /* @var $pModel Mage_Catalog_Model_Product */ foreach ($products as $product_id) { if ($product_id == '') continue; $pModel->unsetData(); $pModel->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'); } }
Good? Ok. Now fire up your browser, and go to: http://magento.installation.com/multiadd/add?products=1,2,3. Replace the CSV list of products at the end of the URL with the product ID’s you want to add to your cart. Once it’s done, it’ll forward you to the checkout/cart page, or if it’s an Ajax request, return a lonely ’1′.
Note: this will only work with Simple Products, as there is no real way of knowing what the options are for Configurable Products, though I’m sure something could be fashioned either with an ajax-based setup/call, or some fancy form trickery to POST them to the controller, then get the foreach($products) loop to set the relevant attributes in the $cart->addProduct() call for each item.
Update: This appears to not work from Magento 1.4 ish and up. I posted an updated/fixed version of the code here.
4 Comments for this entry
1 Trackback or Pingback for this entry
-
Adding multiple products to the cart simultaneously in Magento (Part 2) | danneh.org
September 8th, 2011 on 10:00 pm[...] Dan on Sep.08, 2011, under Magento, PHP A while ago I wrote about adding multiple products to the shopping cart simultaneously. It turns out this seems to have stopped working from Magento 1.4 or so. Up until now, I’ve [...]
June 25th, 2011 on 12:09 pm
hello,
it’s not work for me……
July 7th, 2011 on 3:45 pm
Agreed, code does not work as of 1.4.2
Any ideas?
September 8th, 2011 on 9:10 pm
Hi,
See here for a fixed/working version in 1.4 and up.
Dan
December 6th, 2011 on 6:52 am
Hi Sir, I followed your code and the instructions given but I still can’t get my code to work. I saw the fix for the addcontroller for 1.4 and above but still not working, any ideas please?