Adding a Product to the Cart via Querystring

Last modified by FerdiUSA on Thu, June 24, 2010 16:41
Source|Old Revisions  

This is an old revision of the document!

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 product by SKU we must first instantiate an instance of the Mage::app() outside of the present application. You can do this as such:

include_once 'app/Mage.php';
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.

$cProd = Mage::getModel('catalog/product');
$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:

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

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.

$w = Mage::getSingleton('core/resource')->getConnection('core_write');
$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:

$_product = Mage::getModel('catalog/product')->load($id);
if($_product->getTypeId() == 'bundle'){
  $w = Mage::getSingleton('core/resource')->getConnection('core_write');
  $result = $w->query("select `option_id`, `selection_id` from `catalog_product_bundle_selection` where `parent_product_id`=$id");
  $route = "checkout/cart/add";
  $params = array('product' => $id);
  while($row = $result->fetch(PDO::FETCH_ASSOC)){
      $params['bundle_option['. $row[option_id] .']'][] = $row['selection_id'];
  header("Location: ". Mage::getUrl($route, $params));


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.