title: How to add product to cart programmatically in Magento
body: <p>I am unable to add product to cart programmatically in Magento , I have tried every possible technique to add product with quantity to cart ( in Magento), following are the things I have tried and forums/sites taken help from. But none of them is working. I am using Magento Community edition 1.9.1.1. If anyone has any suggestion/answer, please share.</p>
  
  <ul>
  <li>Tried to do it via URL - e.g. <code>[Magento_Store_URL]/checkout/cart/add?product=[id]&amp;qty=[qty]</code> or even like this -  <code>[Magento_Store_URL]/checkout/cart/add/product/[id]/qty/[qty]</code>. I have also tried with <code>form_key</code>, generated via <code>Mage::getSingleton('core/session')-&gt;getFormKey();</code>. All these stuff is not working at all. These things are mentioned here - <a href="http://www.magentocommerce.com/wiki/4_-_themes_and_template_customization/catalog/adding_a_product_to_the_cart_via_querystring" rel="nofollow noreferrer">Magento website</a> </li>
  <li>Next, I have tried via programmatically, like this.</li>
  </ul>
  
  <p><div class="snippet" data-lang="js" data-hide="true" data-console="false" data-babel="false">
  <div class="snippet-code snippet-currently-hidden">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php 
  require_once 'app/Mage.php'; 
  Mage::app(); 
  $product=new Mage_Catalog_Model_Product();
  $product-&gt;load(1); // 1 is product id, this is simple product ( type)
  $quote = Mage::getSingleton('checkout/session')-&gt;getQuote();
  $quote-&gt;addProduct($product, 1 ); // quantity is 1
  $cart = Mage::getSingleton('checkout/cart');
  $cart-&gt;init(); // tried commenting this too!
  $cart-&gt;addProduct($product, 1 ); // quantity is 1
  $cart-&gt;save();
  Mage::getSingleton('checkout/session')-&gt;setCartWasUpdated(true);
  $quote-&gt;collectTotals()-&gt;save(); header('Location: '. 'http://localhost/magento/index.php/checkout/cart');</code></pre>
  </div>
  </div>
  </p>
  
  <ul>
  <li>Also tried following.</li>
  </ul>
  
  <p><div class="snippet" data-lang="js" data-hide="true" data-console="false" data-babel="false">
  <div class="snippet-code snippet-currently-hidden">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php
  require_once 'app/Mage.php';
  Mage::app();
  $params=array( 'product'=&gt;1, 'qty' =&gt; 3 );
  $cart = Mage::getSingleton('checkout/cart');
  $product = new Mage_Catalog_Model_Product();
  $product-&gt;load($params["product"]);
  $cart-&gt;addProduct($product, $params);
  $cart-&gt;save();
  Mage::getSingleton('checkout/session')-&gt;setCartWasUpdated(true);
  header('Location: '. 'http://localhost/magento/index.php/checkout/cart');</code></pre>
  </div>
  </div>
  </p>
  
  <p>I have taken help from following websites.</p>
  
  <ul>
  <li><a href="http://gzchauhan.blogspot.in/2012/03/programmatically-add-product-in-cart-in.html" rel="nofollow noreferrer">http://gzchauhan.blogspot.in/2012/03/programmatically-add-product-in-cart-in.html</a></li>
  <li><a href="https://stackoverflow.com/questions/19153691/magento-add-a-product-to-the-cart-via-query-string-without-form-key-parameter/19999360#19999360">Magento - Add a product to the cart via query string without form_key parameter</a></li>
  <li><a href="https://magento.stackexchange.com/questions/17997/product-not-adding-to-cart">https://magento.stackexchange.com/questions/17997/product-not-adding-to-cart</a></li>
  <li><a href="https://magento.stackexchange.com/questions/32967/programmatically-adding-product-to-cart-returns-empty-price">https://magento.stackexchange.com/questions/32967/programmatically-adding-product-to-cart-returns-empty-price</a></li>
  <li><a href="http://subasd.com.np/magento-programmatically-add-product-to-cart-with-custom-options/" rel="nofollow noreferrer">http://subasd.com.np/magento-programmatically-add-product-to-cart-with-custom-options/</a></li>
  </ul>
  
  <p>Is there any wrong in my code, whatever I have tried or is there any setting issue with my Magento Installation? </p>
  
  <p><strong>Update 1</strong>
  I have tried below from the answer <a href="https://stackoverflow.com/a/30821628/932584">proposed to this question</a>, still that is <strong>not</strong> working.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="true" data-console="false" data-babel="false">
  <div class="snippet-code snippet-currently-hidden">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>$formKey = Mage::getSingleton('core/session')-&gt;getFormKey();
  $params = array(
      'product' =&gt; 3,
      'qty' =&gt; 2,
   'form_key' =&gt; $formKey
  );
  $product = Mage::getModel('catalog/product')-&gt;setStoreId(Mage::app()-&gt;getStore()-&gt;getId())-&gt;load($params['product']);
  $cart = Mage::helper('checkout/cart')-&gt;getCart();
  $cart-&gt;addProduct($product, $params);
  $cart-&gt;save();</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>Update 2</strong></p>
  
  <p>This is working, if I create a controller and place all code there, not working - in a standalone page that refers /app/Mage.php .</p>