سؤال

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:

ThirdPartyCompany\ThirdPartyModuleName\Model\Carrrier.php

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"
    xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="ThirdPartyCompany\ThirdPartyModuleName\Model\Carrier" type="MyCompany\MyModule\Model\Carrier"/>
</config>

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"
    xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="MyCompany_MyModule">
    <sequence>
        <module name="Magento_Quote"/>
        <module name="ThirdPartyCompany_ThirdPartyModuleName"/>
    </sequence>
</module>
</config>

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?

هل كانت مفيدة؟

المحلول 2

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.

نصائح أخرى

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top