Overriding a core model in Magento

Posted on October 12, 2012 by Nimrod Techn There have been 0 comments

Overriding a core model in Magento can be done by creating a Magento extension:

Let's say we would like to override app/code/core/Mage/Newsletter/Model/Subscriber.php

1. Create the company directory in local (app/code/local/Engineering).

2. Create the extension directory inside (app/code/local/Engineering/Newsletter)

3. Create model & etc directories inside (app/code/local/Engineering/Newsletter/model & app/code/local/Engineering/Newsletter/etc).

model - what we would like to rewrite

etc - the necessary configuration to tell Magento we're overriding a core model

4. Copy the model there (Subscriber.php).

5. Change the copied-model's class name to the following:  class Engineering_Newsletter_Model_Subscriber extends Mage_Newsletter_Model_Subscriber

6. Create a config.xml file in app/code/local/Engineering/Newsletter/etc

7. Place the following inside the config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Engineering_Newsletter>
            <version>0.1.0</version>
        </Engineering_Newsletter>
    </modules>
    <frontend>
        <routers>
            <newsletter>
                <args>
                    <modules>
                      <Engineering_Newsletter before="Mage_Newsletter">
                         Engineering_Newsletter
                      </Engineering_Newsletter>
                   </modules>
                </args>
            </newsletter>
        </routers>
    </frontend>
    <global>
        <models>
            <newsletter>
                <rewrite>
                    <subscriber>Engineering_Newsletter_Model_Subscriber</subscriber>
                </rewrite>
            </newsletter>
        </models>
   </global>
</config>

* Bear in mind: the xml file is case-sensitive, and you'd better stick to the conventions (capitals wherever needed) - that's where alot of beginners waste hours of debugging, trying to understand where their mistake is.


This post was posted in Writing a Magento Extension

Magento is a well-engineered eCommerce platform designed to help engineers develop customized eCommerce online stores. Due to lack of proper coding documentation, Engineer-ing.com was created with the sole purpose of instructing Magento developers to-be with the "how-to-do" know-how. In the event of unresolved issues, you are more than welcome to contact me for consultation. However, please do so only if you possess a Software Engineering background and you're able to specify your question.