Here we have another Magento blog. Recently I was working on building a quick shop module for magento. Its like customer can quickly search products based on some attributes. All the products with matching criteria will be displayed with their options. Customer can select product options and enter quantity and those products get added to shopping cart manually. Here all the products will be configurable products. So I created a custom module for it. After relevant products are displayed all the data like product id, quantity, option id and option values will be sent to cart controller page. This is the page where we need to write code to add product in the cart.
There are two way to do this. Following are the two options.
Option 1 : Via query string
Following is the URL format to add product to shopping cart.
$route = "http://www.your_domain.com/checkout/cart/add?product=272&qty=2&super_attribute=30";
header("Location : ".$route);
Here 272 is the product entity id displayed in magento admin panel. 22 is the attribute associated with this product and 30 is the selected value of that attribute.
Option 2 :
Following is the code to add product to shopping cart.
$params = array(
'product' => 272,
'super_attribute' => array(
22 =>30 ,
'qty' => 2,
$cart = Mage::getSingleton('checkout/cart');
$product = new Mage_Catalog_Model_Product();
Using above code you can add any number of products to the cart.
Above code will work for only configurable products having only one option. If configurable product has more option then all the option values should be specified.