1

I'm writing a small module that will add a product to the cart automatically (based on certain criterias). However if the user subsequently removes that automatic product from the cart I need to know so that I don't add it again in the current session.

Does the cart object hold anything that can tell me if a product has been removed from the cart already?

sulman
  • 2,431
  • 7
  • 40
  • 62

2 Answers2

2

Magento doesn't keep record of which items have been removed, you will have to do that yourself. Start by listening for an event;

app/local/YOURMODULE/etc/config.xml

<config>
...
    <frontend>
        <events>
            <sales_quote_remove_item>
                <observers>
                    <class>YOURMODULE/observer</class>
                    <method>removeQuoteItem</method>
                </observers>
            </sales_quote_remove_item>
        </events>
    </frontend>
...

app/local/YOURMODULE/Model/Observer.php

<?php

class YOU_YOURMODULE_Model_Observer
{
    public function removeQuoteItem(Varien_Event_Observer $observer)
    {
        $product = $observer->getQuoteItem()->getProduct();
        // Store `$product->getId()` in a session variable
    }
}

?>

Create a session class that extends Mage_Core_Model_Session_Abstract and use it to store the product IDs you collect in the above observer. You can then refer to that session object (called by Mage::getSingleton()) to see what products used to be in the cart.

clockworkgeek
  • 37,650
  • 9
  • 89
  • 127
  • Brilliant clockworkgeek that's just what I was after. Thanks very much for that. I'll give it a go when I can! thanks! – sulman Nov 24 '10 at 16:44
0

yes you can get current items in cart like this:-

foreach ($session->getQuote()->getAllItems() as $item) {

    $output .= $item->getSku() . "<br>";
    $output .= $item->getName() . "<br>";
    $output .= $item->getDescription() . "<br>";
    $output .= $item->getQty() . "<br>";
    $output .= $item->getBaseCalculationPrice() . "<br>";
    $output .= "<br>";
}

This link may helpful http://www.magentocommerce.com/boards/viewthread/19020/

Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
  • Thanks for the reply but that doesn't tell me if the item has been removed at some point from the cart. Unless I'm missing something? – sulman Nov 23 '10 at 10:52