0
votes

Here we have firstly truncate our magento cart,

$cart = Mage::getModel('checkout/cart');                
  $cart->truncate()->save(); // remove all active items in cart page
  $cart->init();


  Mage::getSingleton('checkout/session')->clear();

After this, we tried to add products

$cart = Mage::getSingleton('checkout/cart'); 
   $cart->init();
   $cart->addProduct($id,$qty);

It's giving 500 Internal Server Error

Note: It's only giving error when we apply adding product quantity to max.

Ex. available qty. 100 here in my case $qty=100

Note : Our code is working perfectly but only the first time, the rest of the time it's giving this error.

1

1 Answers

0
votes

Found answer for this,

I have found answer,

 $session = Mage::getSingleton('checkout/session');
    $session->getQuote()->delete();
    $session->clear();

    $cart = Mage::getModel('checkout/cart');
    $cart->setQuote($session->getQuote());

    $product = Mage::getModel('catalog/product') -> load($product_id);
    if ($product) {
        if ($product -> isSaleable()) {
            $cart -> addProduct($product, array('qty' => $qty));
        }
    }

    // $session->setCartWasUpdated(true); // Not really necessary - Vicary
    $cart->save();