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.