0

how i can override observer of other module(custom module)

to my another custom modules in magento 2

is it possible ???

Does anyone have an idea?

ex :

custom module 1:

the observer for catalog price change

custom module 2:

i want the price of changed by observer in custom module1.

0

3 Answers 3

1

Yes, you can using preference

<?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="Vendor1\ModuelName1\Observer\FileName1" type="Vendor2\ModuelName2\Observer\FileName2" />
</config>

Just copy the existing observer from FileName1.php to FileName2.php and do alter the namespace and all. and I hope it works.

Despite executing first observer it will execute second observer.

3
  • how to get the product price changed by the observer in another module Apr 5, 2019 at 8:06
  • No that's not possible, guess you're setting custom price in quote so you can get quote again and can change it accordingly. that's possible Apr 5, 2019 at 8:11
  • so i need to add discount logic agin in module 2 observer Apr 5, 2019 at 8:52
0

Using Preference it's is possible

etc/di.xml

<?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="\Vendor\module1\file\location" type="\Vendor\module2\file\location" /> 
</config>
5
  • i have another doubt Apr 5, 2019 at 7:41
  • i have changed the price of product using custom module1(for some discount for customer) Apr 5, 2019 at 7:42
  • after another module 2. i will get the price but it's getting Product orignal price not getting discount price Apr 5, 2019 at 7:43
  • how i can get that discount price in custom module 2?? Apr 5, 2019 at 7:44
  • Hi Rk Rathod, can you pls help me with magento.stackexchange.com/q/336522/57334
    – zus
    May 1, 2021 at 9:22
0

To override an observer, I think better way is to use events.xml rather than using preference.

Suppose you have events.xml in ModuleA

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="some_event">
        <observer name="myObserverName" instance="MyCompany\ModuleA\Observer\MyObserver" />
    </event>
</config>

Now if you want to override the observer MyCompany\ModuleA\Observer\MyObserver, then create events.xml in your custom module as follows

<?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
        <event name="some_event">
            <observer name="myObserverName" instance="MyCompany\CustomModule\Observer\MyObserver" />
        </event>
    </config>

And in your custom module's(MyCompany_CustomModule) module.xml add sequence of MyCompany_ModuleA

<?xml version="1.0" encoding="UTF-8" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="MyCompany_CustomModule">
        <sequence>
            <module name="MyCompany_ModuleA" />
        </sequence>
    </module>
</config> 

Also, Magento recommends to use plugins for data modification. Observers should not be used for data modifications.

2
  • some confusions in name of mycomapany_module and custom module could you plz explain clear ly Apr 5, 2019 at 9:14
  • @prabhakaran7 MyCompany_ModuleA is the module name that you want to override and MyCompany_CustomModule is the module name that you are using to override the MyCompany_ModuleA Apr 5, 2019 at 9:17

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.