0

I have need to get Order information in checkout_submit_all_after event observer in Afterpay Payment Method Module. I am trying to add this event in that module.

File Path: app/code/Afterpay/Afterpay/etc/events.xml

<?xml version="1.0"?>
<!--
/**
 * Magento 2 extensions for Afterpay Payment
 */
-->
<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="afterpay_save_order_after_submit" instance="Afterpay\Afterpay\Observer\SaveOrderAfterSubmitObserver" />
    </event>
</config>

File Path: app/code/Afterpay/Afterpay/Observer/SaveOrderAfterSubmitObserver.php

<?php
/**
 * Magento 2 extensions for Afterpay Payment
 */
namespace Afterpay\Afterpay\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Sales\Model\Order;

class SaveOrderAfterSubmitObserver implements ObserverInterface
{
    /**
     *
     * @var \Magento\Framework\Registry
     */
    protected $coreRegistry;

    /**
     * @param \Magento\Framework\Registry $coreRegistry
     */
    public function __construct(
        \Magento\Framework\Registry $coreRegistry
    ) {
        $this->coreRegistry = $coreRegistry;
    }

    /**
     * Save order into registry to use it in the overloaded controller.
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return $this
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        /* @var $order Order */
        $order = $observer->getEvent()->getData('order');
        $orderId = $order->getId();
        print_r($orderId); die('1');
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/testingObserver.log');

        $logger = new \Zend\Log\Logger();

        $logger->addWriter($writer);

        $logger->info($orderId."OrderId");

    }
}

I only want to use this event. I have checked several other questions where this event is not working but all the answers are asking to use different event. But I don't want to use any other event. Please help me to get work this event in Magento 2.

0

AfterPay placed order inside their own module which use vendor/magento/module-quote/Model/QuoteManagement.php::submit and don't use vendor/magento/module-quote/Model/QuoteManagement.php::placeOrder method that actually fire: checkout_submit_all_after event. You can use sales_model_service_quote_submit_success instead.

For your case, just modify events.xml by the following code:

<?xml version="1.0"?>
<!--
/**
 * Magento 2 extensions for Afterpay Payment
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_model_service_quote_submit_success">
        <observer name="afterpay_sales_model_service_quote_submit_success" instance="Afterpay\Afterpay\Observer\SaveOrderAfterSubmitObserver" />
    </event>
</config>
| improve this answer | |

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.