Search code examples
magentoobserver-pattern

Product Delete Observer


I want to create an observer for Product deletion. Means when admin deletes a product, during deletion I want to add some custom functionality in this process. Currently I'm using

catalog_controller_product_delete

for this purpose. But it is doing nothing for me. Please help me. How can I do something extra during product deletion?

I want to send deleted product id to my API where I have copy of this product so that I can delete it from there too, but it is not triggering the event. I know this because I am triggering sendProductDelReq() method by <method>sendProductDelReq</method> and for verification I've put a die in this method.


Solution

  • For the people that have encountered the same problem and who were hoping to find an answer here.
    I found the problem.
    First of all, how are you deleting the product?
    If you are deleting the product from within the product edit page, chances are your observer won't work.
    If you are deleting the product from within the Product Grid page, your observer will probably work fine.

    The issue is that the event: catalog_controller_product_delete, only gets dispatched in the massDeleteAction() in the productController().
    And NOT inside the deleteAction().

    I've already submitted this issue as a bug at magentocommerce.com/bug-tracking.

    To fix this, paste this:

    Mage::dispatchEvent('catalog_controller_product_delete', array('product' => $product));
    

    Inside your deleteAction(), right before $product->delete();
    Like so:

    public function deleteAction()
    {
        if ($id = $this->getRequest()->getParam('id')) {
            $product = Mage::getModel('catalog/product')
                ->load($id);
            $sku = $product->getSku();
            try {
                Mage::dispatchEvent('catalog_controller_product_delete', array('product' => $product));
                $product->delete();
                $this->_getSession()->addSuccess($this->__('The product has been deleted.'));
            } catch (Exception $e) {
                $this->_getSession()->addError($e->getMessage());
            }
        }
        $this->getResponse()
            ->setRedirect($this->getUrl('*/*/', array('store'=>$this->getRequest()->getParam('store'))));
    }