1

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

Its giving 500 Internal Server Error

Note: Its only giving error when we apply adding product quantity to max.

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

Editedcode :

<?php 
include 'app/Mage.php';
Mage::app();

Mage::getSingleton('core/session', array('name' => 'frontend'));
// Need for start the session
$cartHelper = Mage::helper('checkout/cart');
 $items = $cartHelper->getCart()->getItems();
 echo count($items);
 foreach($items as $item)
 {
  $itemId = $item->getItemId();
  $cartHelper->getCart()->removeItem($itemId)->save();
 }
try {
    $product_id = '7896'; // Replace id with your product id
    $qty = '99'; // max qty with your qty
    $product = Mage::getModel('catalog/product')->load($product_id);
    $cart = Mage::getModel('checkout/cart');
    $cart->init();
    $cart->addProduct($product, array('qty' => $qty));
    $cart->save();
    Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

} catch (Exception $e) {
    echo $e->getMessage();
}

Note : Our code is working perfectly for first time only, rest of all time its giving this error

Kindly help

1
  • put code in trycatch and check for exception Commented Feb 23, 2016 at 7:28

2 Answers 2

1
$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();
1

Try this

UPDATES

require_once("app/Mage.php");    
Mage::app();

try {
    $session = Mage::getSingleton('checkout/session');
    $session->start();
    $session->clear();

    $cart  = Mage::getModel('checkout/cart');
    $cart->init(); 
    $cart->truncate();     

    $product_id = '2'; // Replace id with your product id
    $qty = '99'; // max qty with your qty
    $product = Mage::getModel('catalog/product')->load($product_id);    

    $cart->addProduct($product, array('qty' => $qty));
    $cart->save();

    $session->setCartWasUpdated(true);
    echo "product Added";

} catch (Exception $e) {
    echo "<pre>";
    print_r($e->getMessage());
}
8
  • hello @Minesh Patel, getting this Mage_Core_Exception Object ( [_messages:protected] => Array ( ) [message:protected] => The requested quantity for "RING32GJ105 " is not available. Commented Feb 23, 2016 at 8:25
  • and what is your Qty and Stock Availability in Inventory section for that product ? Commented Feb 23, 2016 at 8:28
  • 99 available qty Commented Feb 23, 2016 at 8:28
  • check and make sure your all Indexing is okay Commented Feb 23, 2016 at 8:30
  • Our code is working perfectly for first time only, rest of all time its giving this error Commented Feb 23, 2016 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.