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.
- Tried to do it via URL - e.g.
[Magento_Store_URL]/checkout/cart/add?product=[id]&qty=[qty]
or even like this -[Magento_Store_URL]/checkout/cart/add/product/[id]/qty/[qty]
. I have also tried withform_key
, generated viaMage::getSingleton('core/session')->getFormKey();
. All these stuff is not working at all. These things are mentioned here - Magento website - Next, I have tried via programmatically, like this.
<?php
require_once 'app/Mage.php';
Mage::app();
$product=new Mage_Catalog_Model_Product();
$product->load(1); // 1 is product id, this is simple product ( type)
$quote = Mage::getSingleton('checkout/session')->getQuote();
$quote->addProduct($product, 1 ); // quantity is 1
$cart = Mage::getSingleton('checkout/cart');
$cart->init(); // tried commenting this too!
$cart->addProduct($product, 1 ); // quantity is 1
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
$quote->collectTotals()->save(); header('Location: '. 'http://localhost/magento/index.php/checkout/cart');
- Also tried following.
<?php
require_once 'app/Mage.php';
Mage::app();
$params=array( 'product'=>1, 'qty' => 3 );
$cart = Mage::getSingleton('checkout/cart');
$product = new Mage_Catalog_Model_Product();
$product->load($params["product"]);
$cart->addProduct($product, $params);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
header('Location: '. 'http://localhost/magento/index.php/checkout/cart');
I have taken help from following websites.
- http://gzchauhan.blogspot.in/2012/03/programmatically-add-product-in-cart-in.html
- Magento - Add a product to the cart via query string without form_key parameter
- https://magento.stackexchange.com/questions/17997/product-not-adding-to-cart
- https://magento.stackexchange.com/questions/32967/programmatically-adding-product-to-cart-returns-empty-price
- http://subasd.com.np/magento-programmatically-add-product-to-cart-with-custom-options/
Is there any wrong in my code, whatever I have tried or is there any setting issue with my Magento Installation?
Update 1 I have tried below from the answer proposed to this question, still that is not working.
$formKey = Mage::getSingleton('core/session')->getFormKey();
$params = array(
'product' => 3,
'qty' => 2,
'form_key' => $formKey
);
$product = Mage::getModel('catalog/product')->setStoreId(Mage::app()->getStore()->getId())->load($params['product']);
$cart = Mage::helper('checkout/cart')->getCart();
$cart->addProduct($product, $params);
$cart->save();
Update 2
This is working, if I create a controller and place all code there, not working - in a standalone page that refers /app/Mage.php .
</div>