Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 906
Charles Avatar asked Mar 03 '14 10:03

Charles


2 Answers

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

like image 188
Pavan Kumar Avatar answered Sep 28 '22 09:09

Pavan Kumar


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.

like image 30
Fran Avatar answered Sep 28 '22 10:09

Fran