2

I have installed a module in Magento 1.9 that rewrites a newsletter model from Magento Core. The config.xml file looks like this:

<models>
    <onestepcheckout>
        <class>MW_Onestepcheckout_Model</class>
        <resourceModel>onestepcheckout_mysql4</resourceModel>
    </onestepcheckout>

    <onestepcheckout_mysql4>
        <class>MW_Onestepcheckout_Model_Mysql4</class>
        <entities>
            <onestepcheckout>
                <table>mw_onestepcheckout</table>
            </onestepcheckout>
        </entities>
    </onestepcheckout_mysql4>

    <newsletter><!--newsletter-->
      <rewrite>                  
          <subscriber>MW_Onestepcheckout_Model_Subscriber</subscriber>                        
      </rewrite>            
    </newsletter>
</models>

Everything is working fine in this moment. The problem is when I want to rewrite this new class, because nothing happened. The config.xml is:

<models>
    <extendonestepcheckout>
        <class>Blugento_ExtendOneStepCheckout_Model</class>
    </extendonestepcheckout>

    <onestepcheckout>
        <rewrite>
            <subscriber>Blugento_ExtendOneStepCheckout_Model_Subscriber</subscriber>
        </rewrite>
    </onestepcheckout>
</models>

And the code from my new class is:

class Blugento_ExtendOneStepCheckout_Model_Subscriber extends MW_Onestepcheckout_Model_Subscriber
{
     public function  getCouponCode()
     {
         if(Mage::getConfig()->getNode('modules/JEGAN_EmailCoupon')) {
             $helperData = new JEGAN_EmailCoupon_Helper_Data();
             return $helperData->generateNewCouponCode('jeganemailtabmenu/jegannewsletteremailcoupon', $this->getCustomerId(), 1);
         }
     }
}

I've created a debugging file test.php and wrote this code inside:

include "app/Mage.php";
Mage::app();

$dModel = Mage::getModel('newsletter/subscriber');
var_dump(get_class($dModel));

This code displays on screen:

string(35) "MW_Onestepcheckout_Model_Subscriber"

but it should display:

string(47) "Blugento_ExtendOneStepCheckout_Model_Subscriber"

I don't know what it doesn't work, can you tell me if there is a problem with this code? Thank you!

2
  • you have wrong name of node in config.xml, try changing onestepcheckuot to onestepcheckout
    – Piyush
    Dec 21, 2017 at 8:22
  • This was only a misspelling. In my original code it was write correctly, only on this post was wrong. So this is not the answer I'm looking for.
    – Ciprian
    Dec 21, 2017 at 9:34

1 Answer 1

1

Change this

<onestepcheckuot>
    <rewrite>
        <subscriber>Blugento_ExtendOneStepCheckout_Model_Subscriber</subscriber>
    </rewrite>
</onestepcheckuot>

to this:

<newsletter>
    <rewrite>
        <subscriber>Blugento_ExtendOneStepCheckout_Model_Subscriber</subscriber>
    </rewrite>
</newsletter>

and make your module depend on the MW_Onestepcheckout module.

How does this work.

When instantiating a model, magento like this Mage::getModel('newsletter/subscriber'); magento looks for the xml node models->newsletter.

if inside this node there is node rewrite->subscriber it will load the class specified in that node.
If not, it will build the class name based on what's after the slash /subscriber.
So if you want your class to be used you have to have the nodes like this newsletter/subscriber not onestepcheckout/subscriber.

It you were to instantiate the model like this Mage::getModel('onestepcheckout/subscriber') you code would work, but the core does not load the model like that.
But your class can still extend the third party module one and you get both functionalities. the one from the third party and your changes. Actually this is the standard way of resolving class rewrite conflicts in magento 1.

And you need to make your module depend on the third party one because the configs are merged for all modules and the modules are loaded in their dependency order.
So you need to make sure your config is merged after the third party one.
Short version: "The second mouse gets the cheese".

2
  • This works for me, but Is there any possible to that without rewriting the magento core class? Only rewriting the onestepcheckout class. @Marius
    – Ciprian
    Dec 21, 2017 at 9:30
  • @Ciprian. I added some explanations in the answer about how rewrites work. I hope i made it clearer. You are basically rewriting the core class, but extending the third party module class that also rewrites the core class. Bafta :)
    – Marius
    Dec 21, 2017 at 9:42

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.