当我从心愿单中删除某个项目时,我需要获取该项目的产品ID。
请参阅下面的代码:
config.xml中
<wishlist_item_save_after>
<observers>
<data_wishlist_delete>
<class>data/observer</class>
<method>deleteFromWishlist</method>
</data_wishlist_delete>
</observers>
</wishlist_item_save_after>
Observer.php
public function deleteFromWishlist()
{
$item = Mage::app()->getRequest()->getParam('item');
$action = Mage::app()->getRequest()->getActionName();
if (($action == 'remove') && !empty($item)) {
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = 'SELECT product_id FROM wishlist_item WHERE wishlist_item_id = ' . $item . ' LIMIT 1';
$id = $readConnection->fetchOne($query);
echo 'Product Id: ' . $id . '<br/>';
} else {
return;
}
}
产品ID未到达此处是因为当我们从心愿单中删除项目时,wishlist_item表中的记录正在删除。
答案 0 :(得分:0)
我们可以通过在wishlist删除事件 controller_action_predispatch_wishlist_index_remove
上添加观察者来解决此问题config.xml中
<events>
<controller_action_predispatch_wishlist_index_remove>
<observers>
<data_wishlist_delete>
<class>data/observer</class>
<method>deleteFromWishlist</method>
</data_wishlist_delete>
</observers>
</controller_action_predispatch_wishlist_index_remove>
</events>
并在Observer.php中更新您的功能,以获得如下所示的产品ID。
public function deleteFromWishlist()
{
$item = Mage::app()->getRequest()->getParam('item');
if($item){
$item = Mage::getModel('wishlist/item')->load($item);
$productId = $item->getProductId();
}
}