Question

please check my code because this code removes all cart item. I want to remove 1 by 1.. please check the code and let me know what is the issue.

<?php
ini_set("default_socket_timeout", 200);
ini_set("max_execution_time", 200);
ini_set('display_errors','On');
error_reporting(E_ALL);

if($_REQUEST['hashkey'] == 'NXqXAgFZA0jiWp5t6+=lGpgWJXEkbo' AND $_REQUEST['custid'] != '' AND $_REQUEST['itemid'] != '')
{
    $customer = $_REQUEST['custid'];
    $item_id = $_REQUEST['itemid'];
    require_once ($_SERVER['DOCUMENT_ROOT'] .'/app/Mage.php');
    Mage::app();
    Mage::app()->getTranslator()->init('frontend');
    Mage::getSingleton('core/session', array('name' => 'frontend')); 

if ($customer) {

       $storeIds = Mage::app()->getWebsite(Mage::app()->getWebsite()->getId())->getStoreIds();

      $quote = Mage::getModel('sales/quote')->setSharedStoreIds($storeIds)->loadByCustomer($customer);

    if ($quote) {
        $collection = $quote->getItemsCollection(false);
        if ($collection->count() > 0) {
            foreach( $collection as $item ) {
                if ($item && $item->getId()) {
                    echo $item->getId();
                    die();
                    $quote->removeItem($item->getId());
                    $quote->collectTotals()->save();
                }
            }
        }
    }  


}    

    $retfinal=array("success"=>"1" ,"message"=>"deleted from cart");
    $result=json_encode($retfinal);
    echo $result;

}else
{
    $ret['success'] = "0";
    $ret['message'] = "This is error message...";
    $result=json_encode($ret);
    echo $result;   
}


?>
Was it helpful?

Solution

It seems that you are not testing if the $item_id matches with the $item->getId() : so, you are deleting all the items.

Try to replace if ($item && $item->getId()) { with if ($item && $item->getId() && $item_id == $item->getId()) { .

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top