0

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

  • Please check log exception to find more info. – Khoa TruongDinh May 24 '16 at 15:28
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();
  • also $name = $customer->getName(); //customer name should be $name = $customer->getFirstName().' '.$customer->getLastName(); //customer name – Pallavi May 26 '16 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 – Anush Swaminathan May 27 '16 at 17:10
  • @Anush, Updated my answer. Try that. Hope that helps – Pallavi Jun 6 '16 at 9:02
1

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, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.