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.

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.

  • how to get the product price changed by the observer in another module – prabhakaran7 Apr 5 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 – Prathap Gunasekaran Apr 5 at 8:11
  • so i need to add discount logic agin in module 2 observer – prabhakaran7 Apr 5 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>
  • i have another doubt – prabhakaran7 Apr 5 at 7:41
  • i have changed the price of product using custom module1(for some discount for customer) – prabhakaran7 Apr 5 at 7:42
  • after another module 2. i will get the price but it's getting Product orignal price not getting discount price – prabhakaran7 Apr 5 at 7:43
  • how i can get that discount price in custom module 2?? – prabhakaran7 Apr 5 at 7:44
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.

  • some confusions in name of mycomapany_module and custom module could you plz explain clear ly – prabhakaran7 Apr 5 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 – Anshu Mishra Apr 5 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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