Magento - 同一事件的2名或更多观察员

时间:2010-08-16 09:04:48

标签: magento magento2

我的观察者中有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;
    }
}

此致 SAURABH

3 个答案:

答案 0 :(得分:14)

没时间测试这个,但乍看之下我会尝试制作

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

答案 1 :(得分:5)

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

答案 2 :(得分:0)

我认为你错过了一些想法,你可以使用观察者为一个事件调用许多函数。但是对于每个事件观察者方法名称应该是不同的,也应该是它的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 shipmentave和方法salesOrderShipmentSaveBefore中看到相同的内容。

只需改变这一点,你就完成了。