Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't override magento core config model

Can't override magento core config model Mage_Core_Model_Config. I have magento 1.9.2.1. Here is config.xml

<global>
    <helpers>
        <peexl_customflatrate>
            <class>Peexl_CustomFlatrate_Helper</class>
        </peexl_customflatrate>
            </helpers>
    <models>
        <peexl_customflatrate>
            <class>Peexl_CustomFlatrate_Model</class>
        </peexl_customflatrate> 
                    <core>
                        <rewrite>
                             <config>Peexl_CustomFlatrate_Core_Config</config>
                        </rewrite> 
                    </core>    

And class Peexl/CustomFlatrate/Model/Core/Config.php

class Peexl_CustomFlatrate_Model_Core_Config extends Mage_Core_Model_Config
{

}

Nothing happens :(

like image 769
Paul Avatar asked Dec 18 '22 19:12

Paul


1 Answers

That's right, you can't.

Magento's class rewrite system works because almost all Magento objects are instantiated via the Mage::getModel static class. However, if an object is created directly via the new method

$foo = new Some_Class_File_Here;

Magento's class rewrite won't be able to replace the class that's instantiated. There's a handful of objects Magento needs to instantiate without the rewrite system. Magento needs to instantiate these classes without the rewrite system because they're the actual classes that implement the rewrite system.

These classes include

self::$_objects = new Varien_Object_Cache;        
self::$_app     = new Mage_Core_Model_App();    
self::$_events  = new Varien_Event_Collection();    
self::$_config  = new Mage_Core_Model_Config($options);

Which includes the Mage_Core_Model_Config class. If you wish to modify the behavior of this class, you have two options.

First, you can create a local code pool override

app/code/local/Mage/Core/Model/Config.php

with an exact copy of the class from app/code/copy/Mage/Core/Model/Config.php, plus your changes. The downside of this is you'll need to manually update this class whenever you upgrade Magento, and if you're not careful you may break functionality that core code relies on.

Second, modern versions of Magento 1 contain the option for an alternative configuration class. Take a look at where Magento instantiates the configuration option

#File: app/Mage.php
protected static function _setConfigModel($options = array())
{
    if (isset($options['config_model']) && class_exists($options['config_model'])) {
        $alternativeConfigModelName = $options['config_model'];
        unset($options['config_model']);
        $alternativeConfigModel = new $alternativeConfigModelName($options);
    } else {
        $alternativeConfigModel = null;
    }

    if (!is_null($alternativeConfigModel) && ($alternativeConfigModel instanceof Mage_Core_Model_Config)) {
        self::$_config = $alternativeConfigModel;
    } else {
        self::$_config = new Mage_Core_Model_Config($options);
    }
}    

You can see that Magento looks for a class name in the $options array's config_model key. You can set this via the index.php bootstrap file

#File: index.php
Mage::run($mageRunCode, $mageRunType, array('config_model'=>'Package_Module_Model_Config'));

This is slightly better than a local code pool override, as Package_Module_Model_Config can extend the base configuration class, and you can change only what you need. However, it does rely on you maintaining your own index.php bootstrap file, which makes it not great for redistribution.

Hope that helps!

like image 195
Alan Storm Avatar answered Dec 29 '22 05:12

Alan Storm