Magento add product to cart in programmatically


Magento add product to cart in programmatically : In magento we sometimes need to add the products in cart programmatically. Model Mage::getModel(‘checkout/cart’) is used to manage the cart in magento. Products can be added programmatically using this model. Here in this tutorial we are going to explain how to add products in magento cart programmatically.


Magento add product to cart in programmatically

Here is syntax to add an item programattically. First load the product object using product id which you want to add in cart. Then initialize the cart using the Mage::getModel(‘checkout/cart’); model and add the product object with quantity in cart’s object.

Magento add product to cart in programmatically:

<?php
$productId = '100'; 
$qty = '3';
$product = Mage::getModel('catalog/product')->load($productId);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$cart->addProduct($product, array('qty' => $qty));
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
?>

The above example will add the product with 3 quantities in the current cart.


Advertisements

Add Comment

📖 Read More