magento cannot override core model

magento cannot override core model

August 24, 2026 · Illustrated author avatarBy Stefan Bogdanescu
magento cannot override core model

Magento: Overriding Core Models the Right Way

Overriding core Magento models is a common requirement for customizing functionality without losing changes during updates. However, it’s crucial to set up the override correctly to ensure your changes are preserved and applied properly.

Understanding the Problem

The main issue here lies in the configuration of the rewrite in the config.xml file of your module. The rewrite tag is nested incorrectly, which leads Magento to use the original core model instead of your custom extension.

Correct Configuration for Model Rewrite

In your app/code/local/[namespace]/Sales/etc/config.xml, modify the global section as follows:

<config>
    <modules>
        <[namespace]_Sales>
            <version>0.1.0</version>
        </[namespace]_Sales>
    </modules>
    <global>
        <models>
            <sales>
                <rewrite>
                    <order_pdf_invoice>
                        [namespace]_Sales_Model_Order_Pdf_Invoice
                    </order_pdf_invoice>
                </rewrite>
            </sales>
        </models>
    </global>
</config>

Notice how the rewrite tag is now nested directly under models>sales. This ensures that Magento correctly recognizes your custom model as a replacement for the core Mage_Sales_Model_Order_Pdf_Invoice.

Additional Corrections

Module Configuration File

In your [namespace]_Sales.xml file, ensure you have the correct XML structure:

<?xml version="1.0"?>
<config>
    <modules>
        <[namespace]_Sales>
            <active>true</active>
            <codePool>local</codePool>
        </[namespace]_Sales>
    </modules>
</config>

Model File

In your app/code/local/[namespace]/Sales/Model/Order/Pdf/Invoice.php, you should only include the necessary files at the beginning of the file:

<?php
include_once Mage::getBaseDir('lib')."/myclass.php";
include_once Mage::getBaseDir('lib')."/another_library.php";

class [namespace]_Sales_Model_Order_Pdf_Invoice extends Mage_Sales_Model_Order_Pdf_Invoice
{
    public function getPdf($invoices = array())
    {
        // Your custom code here
    }
}

Clearing Cache Properly

Remember to clear the cache after making changes. Use the following command in your Magento root directory:

php bin/magento cache:clean

Testing the Rewrite

After making these corrections, you can test if your model is being used by checking the class name returned by get_class() in a controller or block:

$model = Mage::getModel('sales/order_pdf_invoice');
echo get_class($model);  // This should print "[namespace]_Sales_Model_Order_Pdf_Invoice"

Conclusion

Overriding core Magento models requires careful configuration of the rewrite in your module’s config.xml file. Ensuring the correct nesting and clearing the cache properly are crucial steps for successful overriding.

By following these best practices, you can maintain a clean code structure that allows for easy updates while preserving your custom functionality.

For more advanced techniques and detailed explanations on Magento development, consider exploring the resources available at Laravel Company.

Extensions Configuration Orders Errors

Our network