11

I am working on this Magento 2 extension and part of the features is to push information about the orders to an external application using web services. I need to push this information after an order is placed in the checkout.

At the moment I am using the event checkout_onepage_controller_success_action which triggers the method to create the orders in the external app. I placed this event in /etc/frontend/events.xml.

So far this is working but I found an issue with the orders status. Sometimes the orders are still in pending status and some other times the orders are in processing. The reason for this is because the payment method first initialize the orders as pending and after the payment is approved, the orders changed to processing. I want to push only processing orders. It seems that sometimes the event checkout_onepage_controller_success_action runs before the payment is authorized and this is causing the issues.

Any idea how to solve this? how can I make sure the payment processing runs before I run my code in checkout_onepage_controller_success_action?

Following my code:

events.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2017 companyname.com
 */
-->
<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="companyname_order_success" instance="Companyname\Shipping\Observer\CreateCompanynameOrderObserver" />
    </event>
</config>

CreateCompanynameOrderObserver.php

/**
 * Create an order in Companyname when order status match the statuses in the backend
 *
 * @param EventObserver $observer
 * @return void
 */
public function execute(EventObserver $observer){
    $order_ids  = $observer->getEvent()->getOrderIds();
    $order_id   = $order_ids[0];

    //Loading order details
    $orderModel         = $this->_orderFactory->create();
    $order              = $orderModel->load($order_id);
    $shipping_method    = $order->getShippingMethod();
    $order_status       = $order->getStatus();

    if($order_status == 'processing'){
        //Push to external app
    }
}
6

You can try the event

checkout_submit_all_after

It runs after order is submited successfully (and in back-end when you created an order too)

For example, in events.xml

<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="yourcompany_yourmodule_checkout_submit_all_after" instance="YourCompany\YourModule\Observer\ProcessOrder" />
    </event>

</config>

And in the observer

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $order = $observer->getOrder();
    $quote = $observer->getQuote();

    // Do whatever you want here

    return $this;
}
  • This is not working for me. The event is not been fired. I tried with "checkout_submit_all_after" and also with "sales_order_place_after" but the only event I am able to capture with an observer is checkout_onepage_controller_success_action. Any suggestions? – Daniel Silva Oct 17 '18 at 14:37
  • @DanielSilva I had the same problem with events not firing but then it did work when moved from frontend to global. That is, moved from etc/frontend/events.xml to etc/events.xml. This shouldn't happen but it does even though the content is identical. – clockworkgeek Oct 10 at 13:39
  • According to this old issue, Magento can't capture sales_order_* events, the solution is to change "frontend" folder to "webapi_rest". – clockworkgeek Oct 10 at 14:23
2

You can use the sales_order_state_change_before event. It provides access to the order and will allow you to check if the payment was processed. Then, when the payment is processed, it will be fired again, which will allow you to create the company name.

In general, Magento 2 appears to be moving away from events in favor of plugins. While many in the community may not agree with that approach, it's definitely something to keep in mind here.

  • Thanks for the answer @tjons. It seems that there are not much events left in Magento 2. I will try to use sales_order_state_change_before and let you know how it goes. – Eduardo Mar 3 '17 at 2:22
  • Hey @tjons, I tried sales_order_state_change_before instead of checkout_onepage_controller_success_action but it was not working. Not sure where I need to place this event and if it will be triggered during the checkout. I have my event in etc/frontend/events.xml any clue why? – Eduardo Mar 6 '17 at 18:21
1

Please use sales_order_place_after event.

This event call only once and when order is successfully placed from the file vendor/magento/module-sales/Model/Order.php in the below function.

/**
     * Place order
     *
     * @return $this
     */
    public function place()
    {
        $this->_eventManager->dispatch('sales_order_place_before', ['order' => $this]);
        $this->_placePayment();
        $this->_eventManager->dispatch('sales_order_place_after', ['order' => $this]);
        return $this;
    }

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.