Magento - How to check if a product has already been removed from the cart
I'm writing a small module that will add a product to the cart开发者_C百科 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?
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.
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/
精彩评论