1

I used below event for order save after Observer. This is my events.xml

  <event name="checkout_onepage_controller_success_action">
    <observer name="sales_order_save_after" instance="[Vendor]\[Module]\Observer\Ordersave" />
</event>

Here is my Observer code

namespace [Vendor]\[Module]\Observer;
class Ordersave implements ObserverInterface { 
    private $logger;
    private $state;
    public function __construct(
    \Magento\Framework\View\Element\Context $context,       
    \Psr\Log\LoggerInterface $logger,
    \Magento\Framework\App\State $state
) {         
    $this->_layout = $context->getLayout();
    $this->_request = $context->getRequest();
    $this->logger = $logger;        
    $this->_state = $state;
  }
    public function execute(EventObserver $observer) { 
      $this->_state->setAreaCode(\Magento\Framework\App\Area::AREA_GLOBAL);
    try{
        $order_ids = $observer->getEvent()->getOrderIds()[0];
        ....
        .... 
        custom code    
     }catch (\Exception $e) {
        $this->logger->info(' --Exception--  '.$e->getMessage());
     }
    }
}

Event is triggering and i can place order with my custom code written inside try block but after order placed i am getting like

   There has been an error processing your request
Error log record number: Some number

Inside that record i am getting below error.

  Area code is not set "1":"  vendor\/magento\/framework\/View\/Page\/Config.php

What is issue in my code please someone suggest me

2
  • Why you use $this->_state->setAreaCode(\Magento\Framework\App\Area::AREA_GLOBAL);?
    – Sohel Rana
    Commented Aug 24, 2018 at 16:47
  • Because it was throwing error after placing order Commented Aug 27, 2018 at 7:29

4 Answers 4

4

try using the event

sales_order_save_commit_after

Try below code

$orderId = $observer->getEvent()->getOrder()->getEntityId();
1
  • Can you please update me how to get order id inside observer? Commented Aug 27, 2018 at 9:07
0

Using $observer->getObject()->getId() for get orderId

8
  • I am getting order id, that is not my error Commented Aug 24, 2018 at 16:33
  • Where is events.xml file? (path) Commented Aug 24, 2018 at 16:39
  • its inside etc folder of module Commented Aug 27, 2018 at 5:51
  • Magento uses areas to efficiently make web service calls, loading only the dependent code for that area. Examples of areas include the storefront (frontend), Magento Admin (adminhtml, the administration interface), and the WebAPI framework (webapi_rest). Trying with move events.xml to frontend Commented Aug 27, 2018 at 6:21
  • so i need to move events.xml to frontend folder? Commented Aug 27, 2018 at 6:26
0

Change following code ;

 $this->_state->setAreaCode(\Magento\Framework\App\Area::AREA_GLOBAL);

to

 $this->_state->setAreaCode(\Magento\Framework\App\Area::AREA_FRONTEND);

and it should work.

EDIT :

Replace this code in your constructor

$this->_state = $state;

to

   $state->setAreaCode(\Magento\Framework\App\Area::AREA_FRONTEND);

and remove

$this->_state->setAreaCode(\Magento\Framework\App\Area::AREA_GLOBAL);
12
  • After using this code, i am getting error like "Area code already set". Commented Aug 27, 2018 at 6:22
  • Then you should remove the code altogether Commented Aug 27, 2018 at 6:48
  • Yes Once i removed and checked, i am getting error like "Area code is not set "1" if i add getting "Area code already set" error Commented Aug 27, 2018 at 6:52
  • You should set this in the contructor of the class itself, like here - alanstorm.com/magento-2-fixing-area-code-not-set-exceptions Commented Aug 27, 2018 at 6:56
  • can you please update me as per my code, the link have different code and it is incomplete Commented Aug 27, 2018 at 6:57
0

You need to use the following code in your observer:

<?php
namespace Vendor\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
class Ordersave implements ObserverInterface { 
    private $logger;
    private $state;
    public function __construct(
    \Magento\Framework\View\Element\Context $context,       
    \Psr\Log\LoggerInterface $logger,
    \Magento\Framework\App\State $state
) {         
    $this->_layout = $context->getLayout();
    $this->_request = $context->getRequest();
    $this->logger = $logger;        
    $this->_state = $state;
  }
    public function execute(\Magento\Framework\Event\Observer $observer) { 
        try{
            $order_id = $observer->getEvent()->getOrderIds()[0];
            $this->logger->info(' --Exception--  '.$order_id);
        }catch (\Exception $e) {
            $this->logger->info(' --Exception--  '.$e->getMessage());
        }
    }
}

And change the name of observer to something else.

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.