5

I have 2 functions in the same Observer class: setQuotationName and setShippingMethod. They are handled at this event: mynamespace_module_before_send_quotation

Here's the config.xml part concerning these methods:

<mynamespace_quotationcart_before_send_quotation>
    <observers>
       <mynamespace_quotationcustomize_before_send_quotation>
            <class>quotationcustomize/observer</class>
            <method>setQuotationName</method>           <!-- for quotation NAME -->
       </mynamespace_quotationcustomize_before_send_quotation>
    </observers>
</mynamespace_quotationcart_before_send_quotation>
<mynamespace_quotationcart_before_send_quotation>
   <observers>
      <mynamespace_quotationcustomize_before_send_quotation>
           <class>quotationcustomize/observer</class>
           <method>setShippingMethod</method>           <!-- for quotation SHIPPING method -->
      </mynamespace_quotationcustomize_before_send_quotation>
   </observers>
</mynamespace_quotationcart_before_send_quotation>

I also tried putting the 2 <class> and <method> parts in the same event node, like this:

<mynamespace_quotationcart_before_send_quotation>
        <observers>
           <mynamespace_quotationcustomize_before_send_quotation>
                <class>quotationcustomize/observer</class>
                <method>setQuotationName</method>           <!-- for quotation NAME -->
                <class>quotationcustomize/observer</class>
                <method>setShippingMethod</method>          <!-- for quotation SHIPPING method -->
           </mynamespace_quotationcustomize_before_send_quotation>
        </observers>
    </mynamespace_quotationcart_before_send_quotation>

... and also having declared the <class> just once and then the 2 <method>s, but none works flawlessly. Once either one of the methods works and the other doesn't OR one of them works and everything else is broken, etc.

So, how can I declare in the config.xml 2 observer methods that are handlen upon the same event occurrence?

13

The events section in the config (like anything else in the config) are merged into one big thing.
You declared your observers by using the same tag inside the <observers> tag for both of them. In this case both of them are merged into one single observer.
In order to have 2 observers on the same event, that key must be unique inside each event.

Here is an example

<some_event> <!-- event name -->
    <observers> <!-- reserved keyword "observers" -->
        <observer_name> <!-- unique name inside the event "some_event" -->
           <class>...</class> <!-- class alias  -->
           <method>....</method> <!-- method name to be executed -->
        </observer_name>
    </observers>
</some_event>

If you have a second observer for the event "some_event" with the same name "observer_name" the second one will override the class and method for your first one.

This is actually a feature that allows you to disable or change core observers without having to rewrite a core observer class.

In your case just rename the tag mynamespace_quotationcustomize_before_send_quotation for your second observer to something else.
Something like this:

<mynamespace_quotationcart_before_send_quotation>
    <observers>
       <mynamespace_quotationcustomize_before_send_quotation>
            <class>quotationcustomize/observer</class>
            <method>setQuotationName</method>           <!-- for quotation NAME -->
       </mynamespace_quotationcustomize_before_send_quotation>
    </observers>
</mynamespace_quotationcart_before_send_quotation>
<mynamespace_quotationcart_before_send_quotation>
   <observers>
      <mynamespace_quotationcustomize_before_send_quotation_shipping>
           <class>quotationcustomize/observer</class>
           <method>setShippingMethod</method>           <!-- for quotation SHIPPING method -->
      </mynamespace_quotationcustomize_before_send_quotation_shipping>
   </observers>
</mynamespace_quotationcart_before_send_quotation>

or better yet, you can compact it in one single event tag

<mynamespace_quotationcart_before_send_quotation>
    <observers>
       <mynamespace_quotationcustomize_before_send_quotation>
            <class>quotationcustomize/observer</class>
            <method>setQuotationName</method>           <!-- for quotation NAME -->
       </mynamespace_quotationcustomize_before_send_quotation>

       <mynamespace_quotationcustomize_before_send_quotation_shipping>
           <class>quotationcustomize/observer</class>
           <method>setShippingMethod</method>           <!-- for quotation SHIPPING method -->
      </mynamespace_quotationcustomize_before_send_quotation_shipping>
    </observers>
</mynamespace_quotationcart_before_send_quotation>
3
  • 2
    Always such great answers from you! Mersi!
    – Syspect
    Mar 17 '14 at 10:09
  • @marius i have a small query in magento2, we have two custom modules that are using the same event name. I would like to know order of execution how it will happen? (that means which observer file should execute 1 & then which one should execute 2nd). Please clarify Jul 21 '19 at 12:23
  • put some debug code in them and see which ones fire first. but in general, it should not be important which one fires first. they should be independent.
    – Marius
    Jul 21 '19 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.