1

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


?>
0

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()) { .

  • thanks its working – neha rani May 21 at 6:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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