1

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.

1 Answer 1

1

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.

4
  • Or add a dependency in you app/etc/modules/Your_Module.xml so your config is loaded after the the original module
    – Jeroen
    Mar 12, 2015 at 12:06
  • Thanks so much for the reply mam08ixo, and also Jeroen. Unfortunately though, I tried the changes recommended and it still doesn't seem to work (I changed my question to show the updated code). Do you have any other thoughts on why the code might not be working? From all the sites I've visited it looks like it should be working, but so far it just doesn't seem to be affecting anything.
    – Zero Wing
    Mar 13, 2015 at 19:47
  • Remove the observer rewrite, flush cache, try again.
    – mam08ixo
    Mar 14, 2015 at 17:21
  • Hi again @mam08ixo, thanks for the suggestion too, unfortunately though it didn't seem to affect anything, I removed the observer rewrite (you can see the change I made in the code above), but it doesn't seem to have changed anything unfortunately, I also cleared the cache by emptying the var/cache and var/session folders, but it also didn't seem to change anything. Do you happen to have any other thoughts? I've admittedly tried dozens of little changes over the last week, but I can't seem to land on any particular change that actually works.
    – Zero Wing
    Mar 16, 2015 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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