1

I know that I can override/rewrite a model class from my module using the following syntax in the config.xml for my module.:

<config>
    <global>
        <models>
            <moduletooverride>
                <rewrite>
                    <modeltooverride>Mycompany_Mymodule_Model_Myfolder_Myclass</customer>
                </rewrite>
            </moduletooverride>
         </models>
     </global>
</config>

But what if I want this to be conditional (e.g. based on some setting in my modules adminhtml configuration section?)

Is there any syntax for this?

Alternatively is there a way for my rewritten class to get to the class as it was before my rewrite? (to allow it to call its "predecessor".

3 Answers 3

5

There's no built in configuration option that will allow you to conditionally rewrite a class like this.

However, a rewritten class is just an extended class, so all standard OOP rules apply, including using parent:: So something like

class My_Rewritten_Class extends Class_I_Rewrote
{

    public function theMethodIRewrote($param, $options)
    {
        $original_results = parent::theMethodIRewrote($param, $options);
        if(!Mage::getStoreConfigFlag('path/to_my/on_or_off_flag'))
        {
            return $original_results
        }

        //continue with the rewrite
    }
}

Finally, although I've never tried it, you should be able to get a reference to to parsed config options with

$config->Mage::getConfig();

And then manually set or unset your rewrite option using its setOptions method.

4
  • But this (first suggestion) does not turn off rewriting in the sense that if you have two classes that try to rewrite the same core class it does not allow my class to gracefully step aside and let the rewrite of the other class still take place. It will disable my own functionality along with that of the second class in favor of the original magento behavior.
    – epeleg
    Jan 20, 2011 at 7:27
  • As for manipulating the config, I have the feeling that even if this will work some of the time it will depend on the order that config files are read and won't provide a fool proof solution.
    – epeleg
    Jan 20, 2011 at 7:46
  • 2
    Both the above are accurate statements. Jan 20, 2011 at 7:56
  • 1
    Sure does feel a bit hacky but it works quite nicely. Would it not be better to only execute the parent function if the store config deems the rewrite active? This way the function isn't unnecessarily executed if Mage::getStoreConfigFlag('path/to_my/on_or_off_flag') returns true. May 14, 2013 at 14:37
2

There are a couple of good suggestions in this discussion: What is the best way to limit a modules functionality by store or website

In particular, this answer has a short and sweet technique that can be used for more than just store-conditionality.

1

An approach like parent::theMethodIRewrote($param, $options) is not always usable, so try the following quite simple solution:

if (!Mage::helper('mymodule')->isEnabled()){

    class My_Rewritten_Class extends Class_I_Rewrote{} //empty body - nothing rewritten

}else{

    class My_Rewritten_Class extends Class_I_Rewrote{

        public function theMethodIRewrote($param, $options){
              /* method body ... */
        }

        /* other methods ... */

    }

}

In my case (magento 1.6.0.0,php 5.3) it seems to be working.

Your Answer

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

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