How to override magento core model (model class file direct under app/code/core/Mage/Core/Model directory)

14,103

Solution 1

Your config.xml has some mistakes. Follow in this way..

< ?xml version="1.0"?>
<config>
    <modules>
        <my_core>
            <version>0.1.0</version>
        </my_core>
    </modules>
    <global>
       <models>
          <core>
              <rewrite>
                  <store>My_Core_Model_Store</Store>
              </rewrite>
          </core>
       </models>
    </global>
</config>

and also in the model class, You need to extend the Parent Class Name. For Ex: My_Core_Model_Filename extends Mage_Core_Model_Store

Solution 2

For most of them it will be

<modulename> <rewrite><path_to_file>My_Extension_Path_To_Extended_File</path_to_file> </modulename>

So in your case it would be

<core> <rewrite><store>My_Core_Model_Store</store></rewrite> </core>

Although, note that for Mysql4 or Resource files, they are defined under modulename_mysql4or modulename_resource.

Share:
14,103

Related videos on Youtube

Charles
Author by

Charles

Updated on May 25, 2022

Comments

  • Charles
    Charles almost 2 years

    I want to override an core model class, but I cannot find the correct way to do it.

    I have googled a lot, but some of the examples are shown how to override a model which are not located at app/code/core/Mage/Core/Model folder and some of the examples are shown how to override a model which in the sub-dir of Core dir, like app/code/core/Mage/Core/Model/Resources/Eav/Mysql4/.

    I have read the examples, but I still cannot find a way to override it. For example, I want to override app/code/core/Mage/Core/Model/Store.php

    And here is the xml I use, but it is not working. Please help me to find out which part is wrong. Thanks in advance!

    app/etc/models/My_Core

    <config>
        <modules>
            <My_Core>
                <active>true</active>
                <codePool>local</codePool>
            </My_Core>
        </modules>
    </config>
    

    app/code/local/My/Core/etc/config.xml

    <config>
        <modules>
            <My_Core>
                <version>0.1.0</version>
            </My_Core>
        </modules>
        <global>
            <models>
                <my_core_store>
                    <rewrite>
                        <class>My_Core_Model_Store</class>
                    </rewrite>
                </my_core_store>
            </models>
        </global>
    </config>
    

    The new Store.php file is at app/code/local/My/Core/Model/Store.php. And the magento version is 1.8.1.0

  • Charles
    Charles about 10 years
    Hi Fran, I have changed the config.xml according to your solution, and I got a 404 page. And I checked the PHP error log, nothing is recorded.
  • Charles
    Charles about 10 years
    OK, now I know why I am wrong. I didn't extends the parent class. So when we customise the Model, we actually "extend" it, not "override" it.