产品删除观察者

发布于 2024-12-15 07:26:26 字数 348 浏览 5 评论 0原文

我想为产品删除创建一个观察者。意味着当管理员删除产品时,在删除过程中我想在此过程中添加一些自定义功能。目前我正在用于

catalog_controller_product_delete

此目的。但它对我没有任何作用。请帮我。在产品删除过程中如何做一些额外的事情?

我想将已删除的产品 ID 发送到我的 API,其中我有该产品的副本,以便我也可以从那里删除它,但它不会触发该事件。我知道这一点是因为我通过 sendProductDelReq 触发 sendProductDelReq() 方法,并且为了进行验证,我在该方法中放置了一个骰子。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我的痛♀有谁懂 2024-12-22 07:26:26

对于遇到同样问题并希望在这里找到答案的人。
我发现了问题。
首先,您如何删除该产品?
如果您从产品编辑页面中删除产品,您的观察者很可能将无法工作。
如果您从“产品网格”页面中删除产品,您的观察器可能会正常工作。

问题在于,事件:catalog_controller_product_delete,仅在 productController()ma​​ssDeleteAction() 中分派。
并且 >deleteAction()内。

我已将此问题作为错误提交到 magentocommerce.com/bug-tracking。

要解决此问题,请粘贴以下内容:

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

在 deleteAction() 内,位于 $product->delete(); 之前

像这样:

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

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'))));
}
幸福不弃 2024-12-22 07:26:26

今天搜索了一些类似的问题后,由于这个拉取请求。

您可以使用 catalog_product_delete_before 事件,该事件在 Mage_Core_Model_Abstract::_beforeDelete() 中调度。适用于网格和编辑页面中的单独删除。

此事件未显示在 Magento 事件列表 创建使用

grep -rin -B2 -A2 "Mage::dispatchEvent" app/* > events.txt

After searching a bit for a similar issue today I found another event for delete thanks to this pull request.

You can use catalog_product_delete_before event, which is dispatched in Mage_Core_Model_Abstract::_beforeDelete(). Works for individual deletes in both grid and edit page.

This event is not shown on the Magento event list created using

grep -rin -B2 -A2 "Mage::dispatchEvent" app/* > events.txt
悲欢浪云 2024-12-22 07:26:26

您实际上应该使用 catalog_product_delete_beforecatalog_product_delete_after 事件。

更多信息请点击这里https://stackoverflow.com/a/14211286/515268

You should actually be using the catalog_product_delete_before and catalog_product_delete_afterevents.

More on it here https://stackoverflow.com/a/14211286/515268

凡尘雨 2024-12-22 07:26:26

我猜catalog_product_delete_after_done是正确的事件

Mage_Catalog_Model_Product具有以下dispatchEvent:

    public function delete()
{
    parent::delete();
    Mage::dispatchEvent($this->_eventPrefix.'_delete_after_done', array($this->_eventObject=>$this));
    return $this;
}

否则,您可能会遇到产品在Magento中被删除但不在您的API中的情况

I guess catalog_product_delete_after_done is the proper Event

Mage_Catalog_Model_Product has the following dispatchEvent:

    public function delete()
{
    parent::delete();
    Mage::dispatchEvent($this->_eventPrefix.'_delete_after_done', array($this->_eventObject=>$this));
    return $this;
}

Otherwise, you could get the case that the product is deleted in Magento but not in your API

我家小可爱 2024-12-22 07:26:26

您还可以使用

模型删除后

无需修改任何核心文件。

以下是删除后函数调度 model_delete_after 事件的简要说明。

protected function _afterDelete()
{
    Mage::dispatchEvent('model_delete_after', array('object'=>$this));

    Mage::dispatchEvent($this->_eventPrefix.'_delete_after', $this->_getEventData());
    return $this;
}

You can also use

model_delete_after

without modifying any core file.

Here's a brief of the after delete function dispatching the model_delete_after event.

protected function _afterDelete()
{
    Mage::dispatchEvent('model_delete_after', array('object'=>$this));

    Mage::dispatchEvent($this->_eventPrefix.'_delete_after', $this->_getEventData());
    return $this;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文