0

I searched a lot but none of the solutions worked for me, I am trying to delete an Item from observer but if there is only one item in cart it works with removeAllItems() but if there are more than one items then it doesn't work, below is snippet from my code for observer checkout_cart_add_product_complete:

// get the product data and product options chosen
$transport = $observer->getTransport();

// get the current product
$product = $observer->getProduct();

$cart = Mage::getSingleton('checkout/session')->getQuote();
            // get all the cart items
$items = $cart->getAllItems();

// if item count is greater than 1 then remove current product only
if (count($items) > 1) {
    foreach ($items as $item) {
        if ($item->getProduct()->getId() == $product->getId()) {
            try {
                  $itemId = $item->getItemId();
                  $cart->removeItem($itemId)->save();

                  if($item->getParentItemId()) {
                      $cart->removeItem($item->getParentItemId())->save();
                  }

              } catch (Exception $e) {
                  Mage::log('Failed to remove item from cart'.$e.'.');
              }
          }
    }
    $cart->setCartWasUpdated(true);
} else { // else every item

    foreach($items as $item)
    {
        if($item->getProduct()->getId() == $product->getId())
        {

            try {
//                $quote = Mage::getSingleton('checkout/session')->getQuote();
                  $cart->removeAllItems();
                  $cart->save();
                  $cart->setCartWasUpdated(true);
            } catch (Exception $e) {
                  Mage::log('Failed to remove item from cart'.$e.'.');
            }
        }
    }

}

else part works (removes item) but mini cart shows 1 product selected and if you hover on it to see the item it show no products in cart. If part doesn't work at all.

  • Do you get error message in your log? – Jaimin Sutariya Mar 14 '17 at 10:19
  • No I do not get any error messages.. – Sagar Guhe Mar 14 '17 at 10:20
  • @JaiminSutariya I enabled log from admin so there are log messages from other module. – Sagar Guhe Mar 14 '17 at 10:27
  • Ho do you get your value for $product? Please add code as you have used $product->getId() but there is no code where the variable is defined. – Jaimin Sutariya Mar 14 '17 at 10:27
  • I have updated the code... – Sagar Guhe Mar 14 '17 at 10:29
0

For the single item, I am sure you can use this function to remove the item from cart.

$quote->removeAllItems();
$quote->save();

This is the working code for the above question.

public function removeItem(Varien_Event_Observer $observer)
{
    $event = $observer->getEvent();
    $productId = $_SESSION['prescription']['product_id'];
    $result = $_SESSION['prescription']['result'];
if($result == 0)
{
    $cart = Mage::helper('checkout/cart')->getCart();
    $items = $cart->getItems();
    $itemCount = count($items);
    if($itemCount > 1)
    {
        $i = 0;
        foreach($items as $item)
        {
            $i++;
            if($i == $itemCount)
            {
                if($item->getProduct()->getId() == $productId)
                {
                    $itemId = $item->getItemId();
                    $cart->removeItem($itemId)->save();
                }
            }
        }
    }
    else
    {
        foreach($items as $item)
        {
            if($item->getProduct()->getId() == $productId)
            {
                try{
                    $quote = Mage::getSingleton('checkout/session')->getQuote();
                    $quote->removeAllItems();
                    $quote->save();
                }catch (Exception $e)
                {
                    Mage::log('Failed to remove item from cart'.$e.'.');
                }
            }
        }
    }
}
|improve this answer|||||
  • thanks for your answer, what is prescription in $_SESSION var? – Sagar Guhe Mar 15 '17 at 4:39
  • I think you have syntax error in your code, outer if or function is not cosed – Sagar Guhe Mar 15 '17 at 4:51
  • This is not removing first product – Sagar Guhe Mar 15 '17 at 4:57
0

I reference this answer.

event bind checkout_cart_product_add_after

code:

public function removeItem($observer)
{
    $event = $observer->getEvent();
    $productId = $event->getQuoteItem()->getProduct()->getId();
    $cart = Mage::helper('checkout/cart')->getCart();
    $items = $cart->getItems();
    $itemCount = count($items);
    if($itemCount > 0)
    {
        foreach($items as $item)
        {
            if($item->getProduct()->getId() == $productId)
            {
                $itemId = $item->getItemId();
                $item->isDeleted(true);
                $cart->save();
            }
        }
    }
}
|improve this answer|||||

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.