I am Using a third party Module and going to customize a model file in it.

I have tried by overriding the model class but it is not showing any effect.

What i am doing is, just taking one method that is.

private function getPrice() {
        return 123455;

File that i want to override placed at in thirdparty Module:


The Structure of my module is as follows:

Model file placed at: MyCompany\MyModule\Model\Carrier.php

And it has the following code in it:

namespace MyCompany\MyModule\Model;

class Carrier extends \ThirdPartyCompany\ThirdPartyModuleName\Model\Carrier
   private function getPrice() {
       return 10000;

My di.xml file has the following code in it:

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<preference for="ThirdPartyCompany\ThirdPartyModuleName\Model\Carrier" type="MyCompany\MyModule\Model\Carrier"/>

And i have module.xml with the following code in it:

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<module name="MyCompany_MyModule">
        <module name="Magento_Quote"/>
        <module name="ThirdPartyCompany_ThirdPartyModuleName"/>

When i directly change getPrice() function in thirdparty module file and change the return value it is changed on frontend but after overriding in my custom module it making no effect.

What wrong i have doing is there any issue with my code or file structure?

  • Have you enabled the module? Commented Feb 2, 2021 at 10:38
  • Yes Module is already enabled, i have checked the config file as well from CLI. Commented Feb 2, 2021 at 10:49

2 Answers 2


You cannot override private functions. Also, you should only use preference as a last resort, try instead to use plugins to change the behaviour. Please note plugins also cannot be used on private methods, so try looking for a public method that invokes the private method you are trying to override, and use an after or around public on that.

  • So as i am re-writing the class it is not possible? Commented Feb 2, 2021 at 11:20
  • yes, not possible. private methods are hidden inside their class, you cannot extend them. this is because of how extensibility works, not related to Magento/php Commented Feb 2, 2021 at 14:17
  • Actually it is possible, but you have to copy all the Methods from Parent classs that are used in current override function. I did it and working fine! Commented Feb 2, 2021 at 14:23
  • well sure, but you're not extending the parent class, you have new private methods that do the same thing. this will be potentially problematic in the long run, with Magento updates, when your "preference" will eventually become outdated. Commented Feb 2, 2021 at 14:46

So in my case, i was missing the depended method from the parent file,

Like my function getPrice() was using other two methods i also have to copy them into my custom model file and its working fine.

Hope so this will help someone else as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.