问题
Is there an observer which can be used to observe events when a product is removed from the cart? I haven't found any.
What I have found is checkout_cart_update_items_after
which can be used if a product is removed by altering the product count, but not when the user uses the remove button. The only alternative I see in the moment is checkout_cart_save_after
which is used whenever the cart changes. Of course this needs custom logic which check which product was removed. Not perfect.
So is there a better way to watch out for remove events?
回答1:
You can use the sales_quote_remove_item
event, dispatched in Mage_Sales_Model_Quote::removeItem()
.
The removed item is passed to the observer as an argument.
Mage::dispatchEvent('sales_quote_remove_item', array('quote_item' => $item));
To get the associated product model in an event observer, use $observer->getQuoteItem()->getProduct()
.
回答2:
Regarding the question of watching for events (whatever they may be), see Mage_Core_Model_App::dispatchEvent()
. Sample debug/logging code:
public function dispatchEvent($eventName, $args)
{
$argsArray = array();
$logfile = fopen(Mage::getBaseDir().'/var/log/events.log','a');
if(is_array($args)){
foreach ($args as $k => $v){
switch (gettype($v)) {
case 'object':
$v = get_class($v);
break;
case 'array':
$v = 'array';
}
$argsArray[$k] = $v;
}
}
$log = $eventName.":\r\t";
foreach($argsArray as $k => $v){
$log .= $k;
$log .= "\r\t\t".$v;
}
$log .= "\r\r";
fwrite($logfile,$log);
fclose($logfile);
// ...Rest of method...
}
来源:https://stackoverflow.com/questions/9191951/observer-for-removed-items-in-the-cart