Adding a Product to the Cart via Querystring

Last modified by Neo The Coder on Mon, August 16, 2010 01:21
Source|Old Revisions  

In working with Magento oftentimes you find yourself wanting to use the cart, checkout and administrative functions of the application without having to be tied to using all of the underlying CMS functionality as well. Many organizations rely on static html frontends while linking to offsite carts. Using Magento’s own built-in functionality we can add simple and bundled items to the cart easily without any intermediary steps.

Adding a Simple Product

By Magento ID

Simple products are the easiest to add because there are fewer options that need to be passed. The basic structure is:


Where [id] is the Magento ID of the product and [qty] is the Quantity you wish to add.

To add a simple product with custom options simply add options[id]=[value] to the end. The basic structure is:


For example:[12]=57

You can get the options id and value by viewing the source of the simple product page and it’s dropdowns.


To add a product by SKU we must first instantiate an instance of the Mage::app() outside of the present application. You can do this as such:

  1. <?php
  2. include_once 'app/Mage.php';
  3. Mage::app();
  4. Mage::getSingleton('core/session', array('name' => 'frontend'));

By adding a $_GET variable we can get the sku and use an instance of the Product model to get the Magento ID for us.

  1. $cProd = Mage::getModel('catalog/product');
  2. $id = $cProd->getIdBySku("$sku");

Now that we have our ID we can do something useful with it - such as add it to the cart with the default of 1 product as the quantity. You can use other $_GET variables and conditions to simulate the adding of the quanitity to the querystring using the PHP Header Redirect:

  1. header('Location: '. Mage::getUrl('checkout/cart/add', array('product' => $id)));

Adding a Bundle Product

The standard URL format for adding a bundle item to the cart via URL is as such:


We need to get the option_id and the selection_id from the catalog_product_bundle_selection table in our database. The easiest way to accomplish this is through the Core_Resource module. We can use our singleton page in the example above to connect to the database for a one-off data query using PDO.

  1. $w = Mage::getSingleton('core/resource')->getConnection('core_write');
  2. $result = $w->query("select `option_id`, `selection_id` from `catalog_product_bundle_selection` where `parent_product_id`=$id");

This will provide us with our dataset. Using PDO and looping for each individual SKU inside of a given bundle ID we find that the following code will build the parameters for the cart add and then redirect to it:

  1. $_product = Mage::getModel('catalog/product')->load($id);
  3. if ($_product->getTypeId() == 'bundle') {
  4.   $w = Mage::getSingleton('core/resource')->getConnection('core_write');
  5.   $result = $w->query("select `option_id`, `selection_id` from `catalog_product_bundle_selection` where `parent_product_id`=$id");
  6.   $route = "checkout/cart/add";
  7.   $params = array('product' => $id);
  8.   while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
  9.       $params['bundle_option['. $row[option_id] .']'][] = $row['selection_id'];
  10.   };
  12.   header("Location: ". Mage::getUrl($route, $params));
  13. }[528]=55&super_attribute[525]=56

Adding a Configurable Product to the Cart via Querystring

For adding a Configurable Product to the Cart via Querystring you need a url format like following[528]=55&super_attribute[525]=56

Neo The Coder 2010/07/26 10:09


There you have it. A simple strategy for incorporating outside-of-application links to add products to the cart. Please extend this wiki to include Downloadable, Configurable and Grouped items.


As of version 1.3.X, it appears that adding a product to the cart by a URL no longer works. When adding the “Red Chair” from the Magento Demo site via a cart query string, it prompts the visitor to “enable cookies” (

This problem only occurs the first time the visitor tries to add the product to the cart. After the visitor has been to any page within the store, even the enable-cookies page, on subsequent tries it works. However, if the visitor clears their browser history and tries again to add the product to the cart, then the “enable cookies” page is displayed again.

Here is a workaround for the problem until it has been permanently fixed. Change a setting in the “Browser Capabilities Detection” in Magento Admin as follows: Admin→Web→Browser Capabilities Detection→Redirect to CMS-page if cookies are disabled→No.