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 cart quantity programatically
Magento Update quantity of cart programatically

// add Mage file for run php script 
require_once('/app/Mage.php');
Mage::app();

// 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');
	$_cart->init();
	// update cart qty in product  
	$_cart->addProduct($_product, array('qty' => $qty));
	// save qty
	$_cart->save();
	Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
	// 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();
} 
See also  Magento get selected simple product id of configurable product on Product details page

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  81  =  89

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US