Get Customer information from order Magento 2
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.
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.
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.