Magento – Extending an Extension’s Observer Class not working

event-observerextensionsmodel

I've been pulling my hair out to try and figure out the correct way to do this (well, not literally since I'm actually bald… but if I had hair, this problem would probably make me bald soon enough!), and I hope someone can help me out, I'd be so very grateful.

I'm using an extension that has an Observer.php file with a line of code (just one line of code!) I'd like to change. I figured the right way to go about it was to extend the class, but it doesn't seem to be working for me, no matter how many different changes I make to try and get it to work.

Here's a snippet of the Extension's code to get a sense for what I'm trying to change:

/app/code/local/Eternal/AjaxCart/Model/Observer.php

class Eternal_AjaxCart_Model_Observer {
    public function addToCartEvent($observer) {
        // Lots of code... I only want to change 1 line though!
    }
}

/app/code/local/Eternal/AjaxCart/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Eternal_AjaxCart>
            <version>1.0.0</version>
        </Eternal_AjaxCart>
    </modules>
    <global>
        <models>
            <eternal_ajaxcart>
                <class>Eternal_AjaxCart_Model</class>
            </eternal_ajaxcart>
        </models>     
        // Other stuff...   
    </global>
    <frontend>
        <events>
            <checkout_cart_add_product_complete>
                <observers>
                    <eternal_ajaxcart>
                        <type>singleton</type>
                        <class>eternal_ajaxcart/observer</class>
                        <method>addToCartEvent</method>
                    </eternal_ajaxcart>
                </observers>
            </checkout_cart_add_product_complete>
            // Other stuff...
    </frontend>
</config>

And here's all of the code I had written to try and override the addToCartEvent() function:

/app/code/local/Hbs/AjaxCart/Model/Observer.php

class Hbs_AjaxCart_Model_Observer extends Eternal_AjaxCart_Model_Observer {
    public function addToCartEvent($observer) {
      // My code in here
    }
}

/app/code/etc/modules/Hbs_AjaxCart.xml

<?xml version="1.0"?>  
<config>  
    <modules>  
        <Hbs_AjaxCart>  
            <active>true</active>  
            <codepool>local</codepool>  
            <depends>
                <Eternal_AjaxCart>
                </Eternal_AjaxCart>
            </depends>
        </Hbs_AjaxCart>  
    </modules>  
</config> 

/app/code/local/Hbs/AjaxCart/etc/config.xml

<?xml version="1.0"?>
<config>
  <modules>
      <Hbs_AjaxCart>
          <version>1.0.0</version>
      </Hbs_AjaxCart>
  </modules>
  <global>
      <models>
          <hbs_ajaxcart>
              <class>Hbs_AjaxCart_Model</class>
          </hbs_ajaxcart>
      </models>
  </global>
  <frontend>
      <events>
          <checkout_cart_add_product_complete>
              <observers>
                    <eternal_ajaxcart>
                        <type>disabled</type>
                    </eternal_ajaxcart>
                    <hbs_ajaxcart>
                        <type>singleton</type>
                        <class>hbs_ajaxcart/observer</class>
                        <method>addToCartEvent</method>
                    </hbs_ajaxcart>
                </observers>
          </checkout_cart_add_product_complete>
      </events>
  </frontend>
</config>

I checked in the Magento admin panel under System -> Configuration -> Advanced, and it shows the Hbs_Ajaxcart as being enabled. But, when I go to test it on my site, it doesn't work, and just does the extension's original code. Is there something I'm missing, or something I'm doing wrong in this configuration? I've tried all sorts of combinations and changes to see if I could figure out how to get it working, but I'm about to throw in the towel since none of it seems to have worked or affected anything. Any help that anyone can give would be greatly appreciated, thanks very much!

Edit #1: I thought it might be helpful to mention some of the different configuration changes I tried making to get it to work. One thing I tried was to completely remove the tag block in my config.xml (which was the original way I had researched as a means to get it working). I also tried moving the tag block in my config.xml into the tag block. I tried fiddling with the tag casing, making all the references to lowercase to . I also tried removing the tag block from my Hbs_AjaxCart.xml file, but that didn't work either. This is just a very frustrating situation, especially since it's just a single line of code that I want to change. If anyone can help, I'd be very grateful!

Edit #2: I've updated the code in my config.xml file above to reflect one of the suggestions I'd received for disabling the plugin's observer. Unfortunately, it doesn't seem to have worked either.

Edit #3: I've updated the code again in my config.xml for one of the suggestions to remove the observer rewrite, but it's still unfortunately not working.

Best Answer

I appreciate your approach not to modify the third-party extension directly.

For observers, there is usually no need to configure a rewrite. Instead you can just

  • disable the original observer method and
  • enable your own observer method, implementing the necessary behaviour.

To do so, your very own config.xml should contain something like:

<checkout_cart_add_product_complete>
    <observers>
        <eternal_ajaxcart>
            <type>disabled</type>
        </eternal_ajaxcart>
        <hbs_ajaxcart>
            <type>singleton</type>
            <class>hbs_ajaxcart/observer</class>
            <method>addToCartEvent</method>
        </hbs_ajaxcart>
    </observers>
</checkout_cart_add_product_complete>

If the original observer method has no dependencies to other properties or methods within its class, there is not even a need to extend it.

Just make sure that your local config.xml gets loaded after the other module's one. Considering alphabetical order it should do, even if the other module was not created in community code pool. And flush the config cache after editing the file.

Related Topic