9

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

Manashvi Birla
  • 2,837
  • 3
  • 14
  • 28
Saurabh
  • 365
  • 8
  • 18

3 Answers3

15

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>
Alana Storm
  • 164,128
  • 91
  • 395
  • 599
  • This is exactly the problem. You can only have one instance of a named observer per event. Each observer will require it's own name. My standard is to use the module name as the observer name. So and in this case. – Lee Saferite Mar 10 '12 at 20:47
  • @Alan Storm, two years after your answer, is not accepted, but does it works? I'm confronting same issue, 2 different modules, need to capture same event and second module is not working. But if I deactivate the first one then 2nd works. – Yaroslav Sep 28 '12 at 07:50
  • 2
    This answer should be accepted. As observer names need to be unique, `shipmentsave` and `bshipmentsave` will work. Otherwise, the last XML node read will simply overwrite the previous observers with the same name. – laketuna Mar 27 '14 at 15:30
  • I can confirm, simply changing the name of the field in the XML file has no side effects (as it is only used uniquely in that file) and resolves the issue of allowing multiple observers. – dotancohen Dec 05 '21 at 16:16
5

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

Suman-PHP4U
  • 1,185
  • 11
  • 13
0

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.

Jas
  • 188
  • 1
  • 3
  • 16
Vinay Sikarwar
  • 456
  • 7
  • 21