当用户点击对数据库执行某些操作的“清除购物车按钮”时,我想调用事件/观察者。我搜索了很多,但没有找到任何具体的解决方案。 请有人给我解决方案,在magento中哪个活动正在呼叫明确的购物车按钮?
答案 0 :(得分:4)
尝试此活动controller_action_predispatch_checkout_cart_updatePost
。
你的config.xml文件应该是,
<?xml version="1.0"?>
<config>
<modules>
<Packagename_ModuleName>
<version>0.1.0</version>
</Packagename_ModuleName>
</modules>
<global>
<helpers>
<modulename>
<class>Packagename_ModuleName_Helper</class>
</modulename>
</helpers>
<models>
<modulename>
<class>Packagename_ModuleName_Model</class>
<resourceModel>modulename_mysql4</resourceModel>
</modulename>
</models>
<events>
<controller_action_predispatch_checkout_cart_updatePost> <!-- identifier of the event we want to catch -->
<observers>
<controller_action_predispatch_checkout_cart_updatePost_handler> <!-- identifier of the event handler -->
<type>singleton</type> <!-- class method call type; valid are model, object and singleton -->
<class>modulename/observer</class> <!-- observers class alias -->
<method>clearCart</method> <!-- observer's method to be called -->
<args></args> <!-- additional arguments passed to observer -->
</controller_action_predispatch_checkout_cart_updatePost_handler>
</observers>
</controller_action_predispatch_checkout_cart_updatePost>
</events>
</global>
</config>
和Model / Observer.php:
<?php
class Packagename_ModuleName_Model_Observer
{
public function clearCart(Varien_Event_Observer $observer)
{
//execute only in empty the cart function(all items removed )
$updateAction = (string)Mage::app()->getRequest()->getParam('update_cart_action'); if ($updateAction != 'empty_cart') return;
echo "got it"; exit;
//your stuffs goes here.
}
}
注意:清除单个购物车(产品)商品时不会触发。我在本地服务器上测试了它,它工作正常。