Adding a Product to the Cart via Querystring

Last modified by Karen Kilroy 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:

/path/to/app/checkout/cart/add?product=[id]&qty=[qty]

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:

/path/to/magento/checkout/cart/add?product=$id&qty=$qty&options[$id]=$value

For example:

http://magentoserver.com/checkout/cart/add?product=13&qty=1&options[12]=57

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

By SKU

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:

/path/to/app/checkout/cart/add/product/[id]/?bundle_option[[option_id]][]=[selection_id]

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);
  2.  
  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.   };
  11.    
  12.   header("Location: ". Mage::getUrl($route, $params));
  13. }

Conclusion

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.

Disclaimer

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” (demo.magentocommerce.com/enable-cookies).

http://demo.magentocommerce.com/checkout/cart/add?product=52

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.




 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs