3

I'm here domain.com/checkout/onepage/success/

Calling my observer on checkout_onepage_controller_success_action

$order = $observer->getEvent()->getOrder();
echo $order->getCustomerId()
echo $order->getState()

I'm not able to get all information above. How to get?

It gives

Fatal error: Call to a member function getState() on null

  • which event you are calling ? Use sales_order_save_after and you will get status. In checkout_onepage_controller_success_action event , status not available – BornCoder Jan 16 '17 at 7:32
12

checkout_onepage_controller_success_action give you order id's

by using $observer->getEvent()->getOrderIds().

This does not given order object directly so, you are getting the error

Then you need to object by this id

<?php
namespace [VendorName]\[ModuleName]\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;

class [ClassName] implements ObserverInterface
{
    protected $_order;
    public function __construct(
        \Magento\Sales\Api\Data\OrderInterface $order
    ) {
         $this->_order = $order;    
    }

    /**
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
      $orderids = $observer->getEvent()->getOrderIds();

        foreach($orderids as $orderid){
            $order = $this->_order->load($orderid);
        }
    }

}
| improve this answer | |
  • 2
    how to get all order items & customer info? – Jackson Jan 16 '17 at 7:36
  • I tried this solution and I received this error: Fatal error: Uncaught TypeError: Argument 1 passed to Tracy\ShippingMethod\Observer\SendOrderToTracy::__construct() must implement interface Magento\Sales\Api\Data\OrderInterface, none given – Daniel Silva Oct 17 '18 at 14:45
3

Create events.xml at app/code/VendoreName/ModuleName/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_onepage_controller_success_action">
        <observer name="checkout_controller_success_action" instance="VendoreName\ModuleName\Observer\AfterPlaceOrder"  />
    </event>
</config>

Create Observer file at app/code/VendoreName/ModuleName/Observer/AfterPlaceOrder.php

namespace VendoreName\ModuleName\Observer;

use Magento\Framework\Event\ObserverInterface;

class AfterPlaceOrder implements ObserverInterface
{
    /**
     * Order Model
     *
     * @var \Magento\Sales\Model\Order $order
     */
    protected $order;

     public function __construct(
        \Magento\Sales\Model\Order $order
    )
    {
        $this->order = $order;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
       $orderId = $observer->getEvent()->getOrderIds();
        $order = $this->order->load($orderId);

        //get Order All Item
        $itemCollection = $order->getItemsCollection();
        $customer = $order->getCustomerId(); // using this id you can get customer name
    }
}
| improve this answer | |
1

In Magento 2.1.9 this works for me.

etc/frontend/events.xml

<event name="checkout_onepage_controller_success_action">
    <observer instance="Vendor\Module\Observer\Checkout\OnepageControllerSuccessAction" name="vendor_module_observer_checkout_onepagecontrollersuccessaction_checkout_onepage_controller_success_action"/>
</event>

Observer\Checkout\OnepageControllerSuccessAction.php

<?php

namespace Vendor\Module\Observer\Checkout;

class OnepageControllerSuccessAction implements \Magento\Framework\Event\ObserverInterface
{

    public function __construct(\Magento\Sales\Model\Order $order)
    {
        $this->order = $order;
    }

    /**
     * Execute observer
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {

        $orderids = $observer->getEvent()->getOrderIds();

        try {
            foreach ($orderids as $orderid) {
                $order = $this->order->load($orderid);                    
                $order->hold();
                $order->save();
            }
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            // Error logic
        } catch (\Exception $e) {
            // Generic error logic
        }
    }
}

I didn't add the logic why the order is placed on hold, but this will get you close to how to modify each of the current orders and saving them.

| 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.