0

When I remove an item from wishlist, I need to get the product id of that item. Anyone please help me on this.

See my code below:

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;
        }
    }

The product id is not getting here is because the record in the wishlist_item table is deleting when we remove an item from wishlist.

4

Instead of checking routes and putting conditions in your observer event, you can add your observer on wishlist remove event controller_action_predispatch_wishlist_index_remove

<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>

And update your function to get Product Id like below.

public function deleteFromWishlist()
{
    $item = Mage::app()->getRequest()->getParam('item');
    if($item){
        $item = Mage::getModel('wishlist/item')->load($item);
        $productId = $item->getProductId();
    }
}
  • do we have such event controller_action_predispatch_wishlist_index_remove in magento?? Because I'm not aware of it – Pavan Kumar Sep 12 '17 at 8:15
  • @PavanKumar, Its not an actual event in Magento. The thing is that you can use an event on any of your action using controller_action_ prefix. more description is here : stackoverflow.com/a/7510618/1409718 – Jaimin Sutariya Sep 12 '17 at 8:31
  • @JaiminSutariya Thats a great solution. I didnt knew abt this till nw. Thanks a lot :) – Pavan Kumar Sep 12 '17 at 9:14
1

You cannot get your output in this way. Because you are saving the product to the wishlist and immediately you are trying to remove it by using SQL query. It will not work in this way. You need to override the removeAction() of wishlist from the file app\code\core\Mage\Wishlist\controllers\IndexController.php.

public function removeAction()
    {
        if (!$this->_validateFormKey()) {
            return $this->_redirect('*/*');
        }
        $id = (int) $this->getRequest()->getParam('item');
        $item = Mage::getModel('wishlist/item')->load($id);
        if (!$item->getId()) {
            return $this->norouteAction();
        }
        $wishlist = $this->_getWishlist($item->getWishlistId());
        if (!$wishlist) {
            return $this->norouteAction();
        }
        try {
            $event_data_array  =  array('cid' => '123'); //if require
            //defining the custom event
            Mage::dispatchEvent('my_custom_event', $event_data_array);
            $item->delete();
            $wishlist->save();
        } catch (Mage_Core_Exception $e) {
            Mage::getSingleton('customer/session')->addError(
                $this->__('An error occurred while deleting the item from wishlist: %s', $e->getMessage())
            );
        } catch (Exception $e) {
            Mage::getSingleton('customer/session')->addError(
                $this->__('An error occurred while deleting the item from wishlist.')
            );
        }

        Mage::helper('wishlist')->calculate();

        $this->_redirectReferer(Mage::getUrl('*/*'));
    }

Here we have adde our own custom event before deleting the item. So now by using this custom event, you can fetch the product_id by loading the wishlist collection.

problem solved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.