9

I want to retrieve customer information from sales order view observer in adminhtml section, currently i only know how to retrieve order in that observer like this:

namespace Namespace\Module\Block\Adminhtml\Order\View;
class Custom extends \Magento\Backend\Block\Template
{
  public function getCustomAttribute(){
    $order = $this->getParentBlock()->getOrder();

    return $order->getId();
  }
}

i need to retrieve the customer information from that order, if the order is from logged in customer

3
  • Did you try $order->getCustomer()? Jul 4, 2017 at 10:06
  • You can directly access customer details like firstname,lastname,email from the order object . eg. $order->getCustomerEmail(),$order->getCustomerFirstname() Jul 4, 2017 at 10:07
  • @Idham Most welcome Jul 4, 2017 at 10:35

5 Answers 5

13

Inside observer,

$order = $observer->getEvent()->getOrder(); 

$orderFromFront = $order->getRemoteIp();
$guestCustomer = $order->getCustomerIsGuest();
$groupId  = $order->getCustomerGroupId();
$firstname = $order->getCustomerFirstname();
$lastname = $order->getCustomerMiddlename();
$lastname = $order->getCustomerLastname();
$prefix = $order->getCustomerPrefix();
$suffix = $order->getCustomerSuffix();
$dob = $order->getCustomerDob();
$taxvat = $order->getCustomerTaxvat();
$gender = $order->getCustomerGender();

For shipping address, $order->getShippingAddress()->getData()
And for billing, $order->getBillingAddress()->getData()

You can get all type of customer data using observer.

5
  • is $order->getCustomerIsGuest() to check if the order from customer logged in or not? Jul 4, 2017 at 10:19
  • if customer is guest at that time its return 1 otherwise null Jul 4, 2017 at 10:19
  • How do I get custom attribute of customer from order?
    – Manish
    Jul 25, 2018 at 11:06
  • the guest placed an order, after that they have registered, then how do i map that guest order to them
    – Jaisa
    Aug 23, 2018 at 5:06
  • @RakeshJesadiya, $order->getShippingAddress()->getCustomerAddressId(); returning empty for me Dec 15, 2020 at 17:23
3

As per as,magento2 standard,you can inject Sales Collection Factory class,Then Filter that collection by Customer id.

protected $order;
public function __construct(
            \Magento\Sales\Model\Order $order,
        ) {
    $this->_order = $order;
    parent::__construct($context, $data);
}


 public function getCustomAttribute(){
    $order = $this->getParentBlock()->getOrder();
   $orderdetails = $this->order->load( $order->getId());
    return  $orderdetails ->getCustomerId();
  }

or

$order = $this->_objectManager->create('\Magento\Sales\Model\Order')->load($id);
$email = $order->getCustomerId();

But this is not recommended.

2

Use repository. With $order you can do

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customer = $objectManager->create('Magento\Customer\Api\CustomerRepositoryInterface')
    ->getById($order->getCustomerId());

$customer is an instance of Magento\Customer\Model\Data\Customer, just call its get() methods to access customer properties.

1

The following code should be able to give you customer information using order object -:

<?php
use Magento\Customer\Api\CustomerRepositoryInterface;

class Order
{
    /**
     * @var \Magento\Customer\Api\CustomerRepositoryInterface
     */
    private $_customerRepository;

    /**
    * @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
    */
    public function __construct(
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
    )
    {
        $this->_customerRepository = $customerRepository;
    }

    /**
    * @param \Magento\Sales\Api\Data\OrderInterface $order
    */
    public function getCustomerCustomAttributeValue($order){
        return $this->_customerRepository->getById($order->getCustomerId())
                                            ->getCustomAttribute('custom_attribute_code')
                                            ->getValue();
    }
}
1

@Idham Choudry,

you can add below code in to your observer to get customer information.

use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\Session;
use Magento\Sales\Model\Order;

class yourobserver implements ObserverInterface
{
protected $customerSession;
protected $customerrepository;
protected $order;

public function __construct(
     Session $customerSession,
    CustomerRepositoryInterface $customerrepository,
    Order $order
 )
{
    $this->customerSession = $customerSession;
    $this->customerrepository = $customerrepository; 
    $this->order = $order;
}

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $event = $observer->getEvent();
    $orderIds = $event->getOrderIds();

    $order = $observer->getEvent()->getOrder();

    $ordercoll = $this->order->loadByAttribute("entity_id",$orderIds[0]);

    $shippingdetails = $ordercoll->getShippingAddress()->getData();
    $magento_address_id = $ordercoll->getShippingAddress()->getId();
    $billingdetails = $ordercoll->getBillingAddress()->getData();
    $paymentdetails = $ordercoll->getPayment()->getData();
    $shipmentdetails = $ordercoll->getShippingmethod();
    $customerinfo = $this->customerSession->getData();
    $shipping_amount = $ordercoll->getShippingAmount();
    $discount_amount = $ordercoll->getDiscountAmount();
    $discount_description = $ordercoll->getDiscountDescription();
    $applied_rule_ids = $ordercoll->getAppliedRuleIds();

}

}
4
  • 1
    i want to retrieve customer information from order data not from session Jul 4, 2017 at 10:02
  • Is the data you want on the order table or order_address table? Jul 4, 2017 at 10:14
  • I have updated my coding to get order billing and shipping address information. you can get customer information. Jul 4, 2017 at 10:17
  • How to get order item data? Nov 20, 2020 at 7:11

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.