1

I am trying to build a Magento 2 Order Observer in which I am trying to get the Product Details such as Product ID, Product Name, Product SKU, Product Quantity and Customer Details such as Customer ID, Customer Name, Shipping Address & Phone Number.

Below is my Observer.php

<?php

namespace My\Plugin\Observer;

use Magento\Framework\Event\ObserverInterface;

class LogAddMessage implements ObserverInterface {

protected $_logger;

public function __construct(
\Psr\Log\LoggerInterface $logger, array $data = []
) {
    $this->_logger = $logger;
}

public function execute(\Magento\Framework\Event\Observer $observer) {
    $order = $observer->getEvent()->getOrder();
    $order_id = $order->getID();
    $order_number = $order->getIncrementId();
    foreach ($order->getAllItems() as $item) {
            $ProdustIds[] = $item->getProductId();
            $ProdustSku[] = $item->getSku();
            $proName[] = $item->getName(); //product name
            $this->_logger->addDebug('Item Name: ' . $item->getName() . 'Item ID: ' . $item->getProductId());
        }
        $proName = json_encode($proName);
        $ProdustSku = json_encode($ProdustSku);
        $customerId = $order->getCustomerId();
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $customer = $objectManager->create('Magento\Customer\Model\Customer')->load($customer);
        $address = $observer->getShippingAddress();
        $address = json_encode($address);
        $name = $customer->getName(); //customer name
        $this->_logger->addDebug('Order ID: ' . $order_id . ', Products: ' . $proName . ', SKU: ' . $ProdustSku . ', Customer ID: ' . $customerId . ', Customer Name: ' . $name . 'Address: ' . $address);
    }

}

On clicking Place Order in Multi Store Magento Website, magento_website_url/rest/store_code/V1/guest-carts/cart_id/payment-information gives a 500 Internal Server Error

1
  • Please check log exception to find more info. Commented May 24, 2016 at 15:28

2 Answers 2

3

check your below line

$customer = $objectManager->create('Magento\Customer\Model\Customer')->load($customer);

It should be

$customer = $objectManager->create('Magento\Customer\Model\Customer')->load($customerId);

If not solved with this, Post your support log here.

You can see address with

$addressObj = $order->getBillingAddress();

echo '<pre>'; print_r($addressObj);

For Telephone, Try this

$addressObj = $order->getBillingAddress();
$to = $addressObj->getTelephone();
3
  • also $name = $customer->getName(); //customer name should be $name = $customer->getFirstName().' '.$customer->getLastName(); //customer name Commented May 26, 2016 at 11:56
  • I realised this mistake after checking exception.log file. I made the changes and I am able to get the Customer name, but I am still not able to get the customer Address and Phone Number. @Pallavi thanks for your help Commented May 27, 2016 at 17:10
  • @Anush, Updated my answer. Try that. Hope that helps Commented Jun 6, 2016 at 9:02
2

You can easily get customer details, get Billing details, get shipping details using below code

 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);
      $orderid=$order->getEntityId();

      /*get customer details*/

  $custLastName= $orders->getCustomerLastname();
  $custFirsrName= $orders->getCustomerFirstname();
  $ipaddress=$order->getRemoteIp();
  $customer_email=$order->getCustomerEmail();
  $customerid=$order->getCustomerId();

  /* get Billing details */  
  $billingaddress=$order->getBillingAddress();
  $billingcity=$billingaddress->getCity();      
  $billingstreet=$billingaddress->getStreet();
  $billingpostcode=$billingaddress->getPostcode();
  $billingtelephone=$billingaddress->getTelephone();
  $billingstate_code=$billingaddress->getRegionCode();

  /* get shipping details */

  $shippingaddress=$order->getShippingAddress();        
  $shippingcity=$shippingaddress->getCity();
  $shippingstreet=$shippingaddress->getStreet();
  $shippingpostcode=$shippingaddress->getPostcode();      
  $shippingtelephone=$shippingaddress->getTelephone();
  $shippingstate_code=$shippingaddress->getRegionCode();

 /* get  total */

  $tax_amount=$order->getTaxAmount();
  $total=$order->getGrandTotal();

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.