3

We have two modules which fire the same event checkout_submit_all_after.But i need to give a preference for this , i tried giving sequence in module b but it won't work.For ex Module A and Module B triggers the same event, but module B triggers first, my requirement is to trigger Module A and after Module B , is there any way i can do this. And what are the events triggers when order is placed through cron jobs, I have used checkout_submit_all_after, does this event fires if the order is placed through crons?.

Please give me any solution for to go forward. Thank You

0

1 Answer 1

4

At first, question, you want to trigger Module A module observer then fire observer of Module B.

Then below is my solution is of this case.

Disable Module B using xml code.

Then Create around plugin the method ModuleA_ObserverClass::execute(\Magento\Framework\Event\Observer $observer) of Observer of module A.

And on that around Plugin PluginClass::aroundExecute() execute original ModuleA_ObserverClass::execute(\Magento\Framework\Event\Observer $observer) and using $proceed(\ModuleA_ObserverClass $subject,\Callable ,$observer) And after running of origina method , add new method to PluginClass.

And on this new method, copy all code of ModuleB: execute and run after $proceed($observer).

Step1: Disable Observer of Module B

<event name="checkout_submit_all_after">
    <observer name="{ModuleB_Observer_Classs}" instance="{observerClassNameOfModuleB}" />
</event>

Assume above one is observer declaration of ModuleB. First, we have to disable this observer.

Let's create An events.xml at your custom module and disable this by XML node disabled.

<?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="checkout_submit_all_after">
    <!-- Note that observer name should be same  of Module B observer -->
        <observer name="{ModuleB_Observer_Classs}" disabled="true" />
    </event>
</config>

Step2: Create around plugin on ModuleA_ObserverClass::execute()

Create di.xml and declare the around plugin for ModuleA_ObserverClass::execute(\Magento\Framework\Event\Observer $observer).

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> 
    <type name="ModuleA_ObserverClass">
        <plugin name="ModuleA_ObserverClass_My_plugin" sortOrder="10" type="{PluginClass}"/>
    </type>     
</config>

And Plugin code:

<?php

namespace {Namespace};


class {PluginClass} {

    public function aroundExecute(
      \ModuleA_Observer_Classs $subject,
       callable $proceed,
       \Magento\Framework\Event\Observer $observer    
   ){
        //This code run original Merthod
        $proceed($observer);
        $this->replicatedMethodExecuteofObseverOfModuleB($observer);
    }

    public function replicatedMethodExecuteofObseverOfModuleB($observer)
    {
        // Copy all code of `ModuleA_ObserverClass::execute(\Magento\Framework\Event\Observer $observer)`
        // and paste here
    }
}

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.