Magento - How to check if a product has already been removed from the cart

StackOverflow https://stackoverflow.com/questions/4255083

  •  27-09-2019
  •  | 
  •  

문제

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?

도움이 되었습니까?

해결책

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/

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top