Magento - 2 个或更多观察者关注同一事件

发布于 2024-09-14 04:17:06 字数 4526 浏览 4 评论 0原文

我的 2 个观察者正在观察相同的 sales_order_shipment_save_before 事件。 ModuleA 是我首先创建的,然后我创建了 ModuleB

现在只有 ModuleB 的观察者被调用,而不是 ModuleA >。

请让我知道我该如何解决这个问题,并且两位观察员都被召集了。

ModuleA

<?xml version="1.0"?>
<config>
    <global>
        <models>
            <shipmentsave>
                <class>My_Shipmentsave_Model</class>
            </shipmentsave>
        </models>
    </global>
    <adminhtml>
        <events>
            <sales_order_shipment_save_before>
                <observers>
                    <shipmentsave>
                        <type>singleton</type>
                        <class>shipmentsave/observer</class>
                        <method>salesOrderShipmentSaveBefore</method>
                    </shipmentsave>
                </observers>
            </sales_order_shipment_save_before>
        </events>
    </adminhtml>
</config>

Observer.php

<?php

    class My_Shipmentsave_Model_Observer
{
    public function salesOrderShipmentSaveBefore(Varien_Event_Observer $observer)
    {
        $shipment = $observer->getEvent()->getShipment();
        return;
    }
}

ModuleB

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <modules>
    <My_Bshipment>
      <version>0.1.0</version>
    </My_Bshipment>
  </modules>
  <admin>
    <routers>
      <bshipment>
    <use>admin</use>
    <args>
      <module>My_Bshipment</module>
                    <frontName>bshipment</frontName>
    </args>
      </bshipment>
    </routers>
  </admin>
  <adminhtml>
    <events>
      <sales_order_shipment_save_before>
        <observers>
          <shipmentsave>
            <type>singleton</type>
            <class>bshipment/observer</class>
            <method>salesOrderShipmentSaveBefore</method>
          </shipmentsave>
        </observers>
      </sales_order_shipment_save_before>
    </events>
  </adminhtml> 
  <global>
    <models>
      <bshipment>
    <class>My_Bshipment_Model</class>
      </bshipment>
    </models>
    <resources>       
      <bshipment_setup>
        <setup>
          <module>My_Bshipment</module>
        </setup>
        <connection>
          <use>core_setup</use>
        </connection>
      </bshipment_setup>
      <bshipment_write>
        <connection>
          <use>core_write</use>
        </connection>
      </bshipment_write>
      <bshipment_read>
        <connection>
          <use>core_read</use>
        </connection>
      </bshipment_read>
    </resources>
    <blocks>
      <!-- adminhtml>
      <rewrite>
    <shipment_view>My_Bshipment_Block_Adminhtml_Shipment_View</shipment_view>
      </rewrite>
      </adminhtml-->
      <adminhtml>
    <rewrite>
      <sales_order_shipment_view>My_Bshipment_Block_Adminhtml_Shipment_View</sales_order_shipment_view>
    </rewrite>
    <rewrite>
      <sales_shipment_grid>My_Bshipment_Block_Adminhtml_Shipment_Grid</sales_shipment_grid>
    </rewrite>
      </adminhtml>          
      <!-- udropship>
      <rewrite>
    <adminhtml_shipment_view>My_Bshipment_Block_Adminhtml_Shipment_View</adminhtml_shipment_view>
      </rewrite>
      </udropship-->
    </blocks>
    <helpers>
        <bshipment>
            <class>My_Bshipment_Helper</class>
        </bshipment>
    </helpers>

  </global>
</config>

Observer.php

<?php
class My_Bshipment_Model_Observer
{
    public function salesOrderShipmentSaveBefore(Varien_Event_Observer $observer)
    {
        $shipment = $observer->getEvent()->getShipment();
    return;
    }
}

问候, 索拉布

2 of my observer are observing same sales_order_shipment_save_before event. ModuleA was the one i created first and then i created ModuleB

Now only ModuleB's observer is called and not ModuleA.

Please let me know how can i resolve this and both of observers are called.

ModuleA

<?xml version="1.0"?>
<config>
    <global>
        <models>
            <shipmentsave>
                <class>My_Shipmentsave_Model</class>
            </shipmentsave>
        </models>
    </global>
    <adminhtml>
        <events>
            <sales_order_shipment_save_before>
                <observers>
                    <shipmentsave>
                        <type>singleton</type>
                        <class>shipmentsave/observer</class>
                        <method>salesOrderShipmentSaveBefore</method>
                    </shipmentsave>
                </observers>
            </sales_order_shipment_save_before>
        </events>
    </adminhtml>
</config>

Observer.php

<?php

    class My_Shipmentsave_Model_Observer
{
    public function salesOrderShipmentSaveBefore(Varien_Event_Observer $observer)
    {
        $shipment = $observer->getEvent()->getShipment();
        return;
    }
}

ModuleB

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <modules>
    <My_Bshipment>
      <version>0.1.0</version>
    </My_Bshipment>
  </modules>
  <admin>
    <routers>
      <bshipment>
    <use>admin</use>
    <args>
      <module>My_Bshipment</module>
                    <frontName>bshipment</frontName>
    </args>
      </bshipment>
    </routers>
  </admin>
  <adminhtml>
    <events>
      <sales_order_shipment_save_before>
        <observers>
          <shipmentsave>
            <type>singleton</type>
            <class>bshipment/observer</class>
            <method>salesOrderShipmentSaveBefore</method>
          </shipmentsave>
        </observers>
      </sales_order_shipment_save_before>
    </events>
  </adminhtml> 
  <global>
    <models>
      <bshipment>
    <class>My_Bshipment_Model</class>
      </bshipment>
    </models>
    <resources>       
      <bshipment_setup>
        <setup>
          <module>My_Bshipment</module>
        </setup>
        <connection>
          <use>core_setup</use>
        </connection>
      </bshipment_setup>
      <bshipment_write>
        <connection>
          <use>core_write</use>
        </connection>
      </bshipment_write>
      <bshipment_read>
        <connection>
          <use>core_read</use>
        </connection>
      </bshipment_read>
    </resources>
    <blocks>
      <!-- adminhtml>
      <rewrite>
    <shipment_view>My_Bshipment_Block_Adminhtml_Shipment_View</shipment_view>
      </rewrite>
      </adminhtml-->
      <adminhtml>
    <rewrite>
      <sales_order_shipment_view>My_Bshipment_Block_Adminhtml_Shipment_View</sales_order_shipment_view>
    </rewrite>
    <rewrite>
      <sales_shipment_grid>My_Bshipment_Block_Adminhtml_Shipment_Grid</sales_shipment_grid>
    </rewrite>
      </adminhtml>          
      <!-- udropship>
      <rewrite>
    <adminhtml_shipment_view>My_Bshipment_Block_Adminhtml_Shipment_View</adminhtml_shipment_view>
      </rewrite>
      </udropship-->
    </blocks>
    <helpers>
        <bshipment>
            <class>My_Bshipment_Helper</class>
        </bshipment>
    </helpers>

  </global>
</config>

Observer.php

<?php
class My_Bshipment_Model_Observer
{
    public function salesOrderShipmentSaveBefore(Varien_Event_Observer $observer)
    {
        $shipment = $observer->getEvent()->getShipment();
    return;
    }
}

Regards,
Saurabh

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

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

发布评论

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

评论(3

半﹌身腐败 2024-09-21 04:17:06

没有时间对此进行测试,但乍一看我会尝试使

<shipmentsave>

每个模块变得不同。类似

    <observers>
      <shipmentsave>
        <type>singleton</type>
        <class>bshipment/observer</class>
        <method>salesOrderShipmentSaveBefore</method>
      </shipmentsave>
    </observers>

    <observers>
      <bshipmentsave>
        <type>singleton</type>
        <class>bshipment/observer</class>
        <method>salesOrderShipmentSaveBefore</method>
      </bshipmentsave>
    </observers>

No time to test this, but at first glance I'd try making

<shipmentsave>

distinct for each module. Something like

    <observers>
      <shipmentsave>
        <type>singleton</type>
        <class>bshipment/observer</class>
        <method>salesOrderShipmentSaveBefore</method>
      </shipmentsave>
    </observers>

and

    <observers>
      <bshipmentsave>
        <type>singleton</type>
        <class>bshipment/observer</class>
        <method>salesOrderShipmentSaveBefore</method>
      </bshipmentsave>
    </observers>
花辞树 2024-09-21 04:17:06

每个观察者都应该有一个唯一的名字。
在代码中,两个观察者具有相同的名称。
因此,为每个观察者指定唯一的名称。

Each observer should have a unique name.
In the code both observer have the same name.
So give unique name to each observer.

脱离于你 2024-09-21 04:17:06

我认为你错过了一些东西。您可以使用事件的观察者来调用尽可能多的函数。但对于每个事件,观察者的方法名称及其 ID 都应该不同。

<events>
    <sales_order_shipment_save_before>
        <observers>
            <shipmentsave>
                <type>singleton</type>
                <class>shipmentsave/observer</class>
                <method>salesOrderShipmentSaveBefore</method>
            </shipmentsave>
        </observers>
    </sales_order_shipment_save_before>
</events>

<events>
    <sales_order_shipment_save_before>
        <observers>
            <shipmentsave>
                <type>singleton</type>
                <class>bshipment/observer</class>
                <method>salesOrderShipmentSaveBefore</method>
            </shipmentsave>
        </observers>
    </sales_order_shipment_save_before>
</events>

您可以在 XML 中看到 id shipmentsave 和方法 salesOrderShipmentSaveBefore 是相同的。

只要改变这个就可以了。

I think you are missing something. You can call as many functions as you can using observers for an event. But for each event, the observer's method name should be different, as well as its id.

<events>
    <sales_order_shipment_save_before>
        <observers>
            <shipmentsave>
                <type>singleton</type>
                <class>shipmentsave/observer</class>
                <method>salesOrderShipmentSaveBefore</method>
            </shipmentsave>
        </observers>
    </sales_order_shipment_save_before>
</events>

<events>
    <sales_order_shipment_save_before>
        <observers>
            <shipmentsave>
                <type>singleton</type>
                <class>bshipment/observer</class>
                <method>salesOrderShipmentSaveBefore</method>
            </shipmentsave>
        </observers>
    </sales_order_shipment_save_before>
</events>

You can see in the XML that the id shipmentsave and method salesOrderShipmentSaveBefore is the same.

Just change this and you are all done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文