3

i need a complete order array with all items in observer.

here is event : etc/frontend/events.xml

<?xml version="1.0" encoding="UTF-8"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/Event/etc/events.xsd">
<event name="checkout_onepage_controller_success_action">
    <observer name="webhive_sps_controller_success_action" instance="WebHive\SPS\Observer\MyObserver"  />
</event>

Observer file: Observer/Myobserver.php

<?php

namespace WebHive\SPS\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\DataObject as Object;

class MyObserver implements ObserverInterface {

/*protected $_objectManager;

public function __construct(\Magento\Framework\ObjectManager $objectManager)
{
    $this->_objectManager= $objectManager;
}*/


/**
 * @override
 * @see ObserverInterface::execute()
 * @used-by \Magento\Framework\Event\Invoker\InvokerDefault::_callObserverMethod()
 * @see \Magento\Framework\App\Action\Action::dispatch()
 * https://github.com/magento/magento2/blob/dd47569249206b217e0a9f9a9371e73fd7622724/lib/internal/Magento/Framework/App/Action/Action.php#L91-L92
    $eventParameters = ['controller_action' => $this, 'request' => $request];
    $this->_eventManager->dispatch('controller_action_predispatch', $eventParameters)
 * @param \Magento\Framework\Event\Observer $observer
 * @return void
 */
public function execute(\Magento\Framework\Event\Observer $observer) {

     $event = $observer->getEvent();
     $orderIds = $event->getOrderIds();

    $order_id = $orderIds[0];   
    echo $order_id;                  // prints current order id (e.g : 7)
    // Here i need some function like getOrderDetails that prints order array
}
}
4
<?php
namespace EwWebHive\SPS\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
class MyObserver implements ObserverInterface
{ 

    protected $orderRepository;

    public function __construct(  
    OrderRepositoryInterface $OrderRepositoryInterface
    ) {
        $this->orderRepository = $OrderRepositoryInterface;

    }

    public function execute(\Magento\Framework\Event\Observer $observer) 
    {
           //Custom log:
           $writer = new \Zend\Log\Writer\Stream(BP.'/var/log/stackexchange.log');
           $logger = new \Zend\Log\Logger();
           $logger->addWriter($writer);

           $order_ids = $observer->getEvent()->getOrderIds()[0];
           $order = $this->orderRepository->get($order_ids);           
           $order_id = $order->getIncrementId();   
           $customer_email = $order->getCustomerEmail();

          $logger->info("latest order Id ===>".$order_id.'customer Email ==>'.$customer_email);

          foreach ($order->getAllVisibleItems() as $item)
          {
                $logger->info("Item Name ===>".$item->getName());
                $logger->info("Item Sku  ===>".$item->getSku());

          } 

    }


}
| improve this answer | |
  • I am getting Area code is not set error – jafar pinjar Aug 27 '18 at 7:44

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.