Can't override magento core config model

若如初见. 提交于 2019-12-10 07:58:14

问题


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 :(


回答1:


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!




回答2:


First of all, your rewrite won't work because it's been set up in wrong way. The rewrite class name in config.xml doesn't match class name in the file, there is word "Model" missing. It should be:

                <rewrite>
                     <config>Peexl_CustomFlatrate_Model_Core_Config</config>
                </rewrite> 


来源:https://stackoverflow.com/questions/35803855/cant-override-magento-core-config-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!