How to override magento function?

How to override magento function?

September 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to override magento function?

Overriding Magento Functions: A Practical Guide

As a senior developer at Laravel Company, I often receive inquiries about how to override specific functions within the Magento framework. In this post, we’ll walk through an example of overriding the getPrefix() and getTelephone() functions in the \app\code\core\Mage\Checkout\Block\Onepage\Billing.php file to use customer-selected prefix and telephone values from the registration page on the checkout billing address.

Why Override Magento Functions?

Overriding core Magento functions allows you to modify their behavior without directly changing the original files. This approach ensures that your customizations remain intact during updates and provides a cleaner separation between your code and Magento’s core functionality.

The Challenge: Modifying Checkout Billing Address

To use the prefix and telephone values from the customer registration page in the checkout billing address, we need to override the getPrefix() and getTelephone() functions in the \app\code\core\Mage\Checkout\Block\Onepage\Billing.php file. Let’s dive into the step-by-step process.

Step 1: Create a Custom Module

Before overriding any core Magento function, it is essential to create a custom module where you’ll store your override files. This approach follows the best practices for maintaining a clean and organized codebase.

To create a new module:

  1. Navigate to app/code/local (or vendor/magento/module-yourmodule if you’re using Magento 2).
  2. Create a folder named after your module, e.g., MyCompany_Checkout.

Step 2: Create the Override Files

Now that we have our custom module ready, let’s create the necessary override files for the \app\code\core\Mage\Checkout\Block\Onepage\Billing.php file.

Step 2.1: Create a Copy of the Original File

First, copy the original \app\code\core\Mage\Checkout\Block\Onepage\Billing.php file and paste it into your custom module folder:

cp /path/to/your/magento/installation/app/code/core/Mage/Checkout/Block/Onepage/Billing.php MyCompany_Checkout/Block/Onepage/

Step 2.2: Rename the File

Rename the copied file to include the local prefix:

mv Billing.php local_Billing.php

Step 3: Override the Functions

Open the renamed local_Billing.php file in your text editor, and let’s override the getPrefix() and getTelephone() functions.

Step 3.1: Overriding getPrefix()

Locate the getPrefix() function within the Mage_Checkout_Block_Onepage_Billing class:

public function getPrefix()
{
    // Original code here...
}

Replace the original code with your custom logic to retrieve the desired prefix value from the customer’s registration data. For example, you can use Magento’s registry to fetch the registered prefix:

public function getPrefix()
{
    $prefix = Mage::registry('customer_session')->getCustomer()->getPrefix();

    return $prefix;
}

Step 3.2: Overriding getTelephone()

Similarly, find the getTelephone() function in the same class:

public function getTelephone()
{
    // Original code here...
}

Replace the original code with your custom logic to retrieve the telephone value from the customer’s registration data:

public function getTelephone()
{
    $telephone = Mage::registry('customer_session')->getCustomer()->getTelephone();

    return $telephone;
}

Step 4: Clear Magento Cache

After making these changes, don’t forget to clear your Magento cache to ensure that the overrides take effect:

rm -rf var/cache/*

Or, if you’re using Magento 2, run this command in your terminal:

bin/magento cache:clean

Step 5: Test Your Overrides

Finally, test your changes by performing a customer registration and checking the billing address during checkout. The prefix and telephone fields should now display the values from the registration page.

Conclusion

In this post, we explored how to override Magento functions using an example of modifying the getPrefix() and getTelephone() functions in the \app\code\core\Mage\Checkout\Block\Onepage\Billing.php file. By following the step-by-step process outlined above, you can effectively customize Magento’s core functionality while maintaining a clean and organized codebase.

Remember to keep your overrides separate from the original files by using custom modules, and always clear the cache after making changes. This approach ensures that your customizations remain intact during updates and provides a better separation between your code and Magento’s core functionality.

At Laravel Company, we specialize in providing personalized solutions for e-commerce platforms like Magento. If you require further assistance or need help with other aspects of Magento development or e-commerce strategy, feel free to contact us at https://laravelcompany.com. Our expert team is always ready to assist you in optimizing your online business.

Happy coding!

Checkout Extensions Customers Errors

Our network