Magento 2 – Get Customer Information from Order

customer-attributemagento2sales-order

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

Best Answer

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.

Related Topic