1

I am new to magento 2. I am trying to add observer that will be triggered after order has been placed.

I have save some certain data to the cookies on my magento 2 website, i have intention to save it to database after the order has been placed already have the table created on the database. Just wanted it and some order details save to database when order has been placed

1
  • User event checkout_onepage_controller_success_action – Jackson Apr 24 '17 at 2:33
0

checkout_onepage_controller_success_action is the best event if you have placed order from frontend.

This event is fire when we customer place an order and goes to order success page .

This event give us order id of current place order of a customer and then at observer using order interface you can get order details.

<?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);
        }
    }

}

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.