Tuesday, January 3

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.

// 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();

No comments:

Post a Comment

10 Best Chatting Apps In India

  The modern world is all about Internet. There was a time when people use to pay high telephone bills to stay in touch with their friends a...