Adding multiple products to the cart simultaneously in Magento

Advertisements

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:



	
		
			true
			local
		
	

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:



	
		
			1.0
		
	
	
		
			
				standard
				
					BTS_AddMultipleProducts
					multiadd
				
			
		
	

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.

Join the Conversation

6 Comments

  1. 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?

Leave a comment

Your email address will not be published. Required fields are marked *