0

This is my app/code/Nadine/Campaign/etc/events.xml

<?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">
        <observer name="nadine_campaign_saa" instance="Nadine\Campaign\Observer\SubmitAllAfter"/>
    </event>
</config>

This is my observer (app/code/Nadine/Campaign/Observer/SubmitAllAfter.php)

<?php
declare(strict_types=1);

namespace Nadine\Campaign\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer as EventObserver;

class SubmitAllAfter implements ObserverInterface
{


    public function execute(EventObserver $observer)
    {
        $handle = fopen('/home/mytest/public_html/staging/var/logghetto.txt', 'a');
        fwrite($handle, "Start my observer SubmitAllAfter alle ore ".date("H:i:s"));
        fwrite($handle,"\n====\n");
        
        fclose($handle);
        die('test in progress');
    }
}

But, when I send an order, this observer don't works. On /vendor/magento/module-quote/Model/QuoteManagement.php I see that, with this code:

$this->eventManager->dispatch('checkout_submit_all_after', ['order' => $order, 'quote' => $quote]);

the event is correctly invoked.

1
  • It will work, if you change the area to webapi_rest as answered below.
    – Haijerome
    Commented Feb 19, 2021 at 15:48

1 Answer 1

0

Update

change the events.xml area from global to webapi_rest

use

app/code/Nadine/Campaign/etc/webapi_rest/events.xml

instead of

app/code/Nadine/Campaign/etc/events.xml

It will work


Better observe the event checkout_onepage_controller_success_action, in case if you want to observe the store front order placement and use one page checkout

<?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_onepage_controller_success_action">
        <observer name="nadine_campaign_saa" instance="Nadine\Campaign\Observer\SubmitAllAfter"/>
    </event>
</config>

Magento\Checkout\Controller\Onepage\Success

$this->_eventManager->dispatch( 'checkout_onepage_controller_success_action', [ 'order_ids' => [$session->getLastOrderId()], 'order' => $session->getLastRealOrder() ] )

Reference

4
  • 1
    Hi there @Haijerome, why is it necessary to change the area to webapi? Some kind of bug or something?
    – Diana
    Commented Feb 22, 2021 at 6:23
  • 1
    Hi @DianaBotean , order placement is handled by REST API (webapi) so it can be observed only from webapi area. It's not a bug BTW. You can find more details about this in this thread github.com/magento/magento2/issues/1581#issuecomment-126400100
    – Haijerome
    Commented Feb 23, 2021 at 5:45
  • thanks! yeah, well coming from m1 development, clearly i still need to adjust to m2 :)
    – Diana
    Commented Feb 23, 2021 at 7:18
  • Yes totally agreed, M1 keeps it simple , whereas M2 keeps it much organized and structured. I'm still not feeling like home with M2 with some areas, though its nice to work with :-)
    – Haijerome
    Commented Feb 23, 2021 at 10:10

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.