Magento – Update cart quantity programatically

Magento – Update cart quantity programatically

In this post we will show you how to Update cart quantity programatically in magento. by using this code you can Update cart quantity programatically. for execute this code you have to create update-cart-quantity.php(create file in root of Magento) programatically and pest code in file and pass quantity($qty) and product id($item_id) in variable.

Magento Update quantity of cart programatically

// add Mage file for run php script 

// pass your qty
$qty = "5";
// pass your id of product
$item_id =  915;

Mage::getSingleton('core/session', array('name' => 'frontend'));
try {
	//  pass your product id
	$product_id = $item_id; 
	// pass your qty in int
	$qty = $qty; 
	// get product detail 
	$_product = Mage::getModel('catalog/product')->load($product_id);
	// get cart detail 
	$_cart = Mage::getModel('checkout/cart');
	// update cart qty in product  
	$_cart->addProduct($_product, array('qty' => $qty));
	// save qty
	// set success message
	Mage::getSingleton('core/session')->addSuccess('Product added successfully');
	// page readdir to cart page
	header('Location: ' . 'index.php/checkout/cart/');
catch (Exception $exception) 
	// print exception of code
	echo $exception->getMessage();
Exit mobile version