3

This is triggered on the checkout_cart_add_product_complete event.

Firstly the code in the if($itemCount > 1) statement works absolutely fine and as expected (it removes the last item/item just added to cart). However the code in the else statement does not.

The item is a simple product.

The else statement is triggered when the customer has an empty cart, and is adding a first product to it which happens to be an item that has failed a check. So it needs to be removed from the cart, leaving them with an empty cart (and some error messages eventually).

However, the else statement does not want to delete that first and only item from the cart. You will be able to see all of the other methods I have tried as I have just commented them out.

I use XDebug and have been stepping through the code, so I can assure you that it makes it into the statement and executes $cart->removeItem($itemId)->save(); - However no errors are shown in logs or from the catch.

I'm at a loss with this, tried so many things but all to no avail. Please someone shed some light & make what I want to achieve possible! Thanks.

Code:

    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{
                        $itemId = $item->getItemId();
                        $cart->removeItem($itemId)->save();
                        //Mage::getSingleton('checkout/cart')->truncate()->save();
                        //$quote = Mage::getSingleton('checkout/session')->getQuote();
                        //$quote->removeItem($itemId)->save();
                    }catch (Exception $e)
                    {
                        Mage::log('Failed to remove item from cart'.$e.'.');
                    }
                }
            }
        }
    }
}
6

So after many hours battling I have managed to solve the problem. Hopefully this will save people many hours trying to achieve the same thing.

I found that if it is the only item in the cart the easiest thing to do is to just clear the quote with

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

So here is the working code:

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.'.');
                }
            }
        }
    }
}
  • what if there's more than one item in the cart? – Joey Oct 24 '17 at 9:40
  • If there is more than one item in the cart, the code loops through the cart and deletes the last item whose Product ID matches $productId. See this section of the code: Delete Last Item. Alternatively you could remove the last item check, but then if there is another product with the same ID in the cart it will remove that too: Delete Item. The first gist works for me as the item I need to remove would always be the last item added to the cart. – Matt Jan 17 '18 at 13:03

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.