4

I am trying to get order,customer,product details in observer. I am using event sales_order_save_after.

<?php
namespace Gworks\Sms\Model\Observer;
use Magento\Framework\Event\ObserverInterface;
class AfterPlaceOrderObserver implements ObserverInterface
{
    protected $_logger;
    public function __construct(
    \Psr\Log\LoggerInterface $logger, //log injection
    array $data = []
    ) {
    $this->_logger = $logger;
       // parent::__construct($data);
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    { 
     //order id
     $order = $observer->getEvent()->getOrder();
     $order_id = $order->getIncrementId();
     $this->_logger->info($order_id);

     //product id and name
     foreach($order->getAllItems() as $item)
     {
     $ProdustIds[]= $item->getProductId();
     $proName[] = $item->getName(); // product name
     }
     foreach($proName as $name)
     {
       $this->_logger->info($name);
     }
     foreach($ProdustIds as $id)
     {
       $this->_logger->info($id);
     }

     //customer id
     $customerid = $order->getCustomerId();
     $this->_logger->info($customerid);

     //customer name
     $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
     $customer = $objectManager->create('Magento\Customer\Model\Customer')->load($customer);
     $name=$customer->getName();
     $this->_logger->info($name);
    } 
}

I need to get below details.

  1. customer name
  2. phone number
  3. ordered product name
  4. shipping address

I have to collect customer name and product name in system.log but I stuck with get shipping address and phone number in log.

  • try to catch sales_order_place_after event as the required detail may not be available in the observer at the event you are catching(sales_order_save_after), because all the data is already passed at sales_order_save_after event.. use sales_order_place_after.. – Mohammad Mujassam Apr 27 '16 at 10:28
  • ND17 answer is correct you can't get product details directly from sales_order_place_after, you can get by using foreach() function. – Bojjaiah Apr 27 '16 at 11:30
11

You can't get product id like this $observer->getEvent()->getProduct()->getId();

This is order event not product event

 public function execute(\Magento\Framework\Event\Observer $observer)
{
 $order = $observer->getEvent()->getOrder();
 $order_id = $order->getIncrementId();
 $this->_logger->info($order_id);   
 foreach($order->getAllItems() as $item){
      $ProdustIds[]= $item->getProductId();

     $proName[] = $item->getName(); // product name
 }
 $this->logger->info($ProdustIds);
 $shippingAddress = $order->getShippingAddress(); // shipping address
 $customer = $order->getCustomerId(); // using this id you can get customer name
} 

And for phone number you can get from shipping address or using customer id

  • thanks It partially works after some modification, I have updated my question. – Bilal Usean Apr 27 '16 at 14:13
  • all the details I have collected except shipping address, still my question alive if I tick its over :-) – Bilal Usean Apr 28 '16 at 4:48
  • Sorry i didn't notice that – ND17 Apr 28 '16 at 5:32
  • in shipping address what is problem? – ND17 Apr 28 '16 at 5:33

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.