How to override a Magento function in app/code/core/Mage/Core/functions.php

How to override a Magento function in app/code/core/Mage/Core/functions.php

July 12, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to override a Magento function in app/code/core/Mage/Core/functions.php

Overriding Magento Core Functions: A Comprehensive Guide

[INTRO]

As a Magento developer, you’ve likely faced the challenge of needing to override core functions that don’t belong to any specific class or module. The file app/code/core/Mage/Core/functions.php is one such location where critical business logic resides without any associated class.

In this post, we’ll explore how to safely and effectively override these functions in Magento 2. We’ll provide a step-by-step guide that adheres to best practices and ensures the stability of your customizations.

[OVERRIDING CORE FUNCTIONS]

Understanding the Problem

The primary issue is that core functions like Mage::app()->getLocale()->currency->setCurrency() don’t belong to any specific class. This makes it challenging to override them using standard Magento extension mechanisms like plugins or observers.

Moreover, these are deeply nested methods that form part of the Magento framework’s fundamental structure. Modifying them directly can lead to unintended consequences and compatibility issues with future Magento updates.

The Solution: Function Override in app/code/Your_Company/Your_Module/etc/di.xml

To override a core function safely, you need to create a new function in your custom module with the same name as the one you want to replace. Then, use Magento’s dependency injection (DI) capabilities to redefine where the function is loaded from.

Here’s how:

  1. Navigate to app/code/Your_Company/Your_Module/etc/di.xml. If this file doesn’t exist, create it.

  2. Add the following XML code inside the <config> tags:

<type name="Magento\Framework\App\Bootstrap">
    <arguments>
        <argument name="functions" xsi:type="array">
            <item name="your_function_name" xsi:type="string">Your_Company\Your_Module\Observer\YourFunction</item>
        </argument>
    </arguments>
</type>

Replace Magento\Framework\App\Bootstrap, functions, and your_function_name with the actual function you want to override. For example, if you’re overriding the core function Mage::app()->getLocale()->currency->setCurrency(), replace your_function_name with currency_set_currency.

  1. Create a new file at app/code/Your_Company/Your_Module/Observer/YourFunction.php. This is where your overridden function will reside.

  2. Implement the new function in YourFunction.php:

namespace Your_Company\Your_Module\Observer;

class YourFunction
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        // Your custom code goes here

        // Call the original function (optional but recommended for stability)
        $originalFunction = new \ReflectionMethod('\Mage\Core\Model\Locale', 'currency_set_currency');
        return $originalFunction->invokeArgs(null, func_get_args());
    }
}

In this example, the execute method contains your custom logic. It optionally calls the original function using PHP’s reflection API to ensure that the core functionality remains intact.

[BEST PRACTICES AND TIPS]

Tip 1: Use a Unique Module Name

Ensure your module name is unique across all installed Magento instances. This helps avoid naming conflicts with other extensions or future updates.

Tip 2: Replicate Core Functionality

Whenever possible, replicate the core function’s behavior in your override. This maintains consistency and reduces the risk of unexpected side effects.

[CONCLUSION]

Overriding core functions in Magento can be a complex task that requires careful consideration. By following this guide, you can safely modify these critical functions while minimizing the risks associated with direct file edits.

Remember, keeping your customizations separate from the core codebase ensures easy upgrades and reduces the likelihood of conflicts. Always strive to follow Magento’s extension mechanisms and best practices for a robust and maintainable solution.

For more advanced topics on Magento development and optimization strategies, don’t forget to explore our comprehensive resources at Laravel Company. We regularly publish articles covering everything from performance tuning to custom theme development to help you master the platform.

Magento 2 Extensions Pricing Errors

Our network