1

How to get checkout page information (firstname, lastname, address etc of customer) , I have created one phtml file and the code is below

$address = $objectManager->get('\Magento\Sales\Model\Order');

        echo   $address->getBillingAddress()."<br/>";  
        echo   $address->getShippingAddress()."<br/>";  

I need to get customer information irrespective of his/her login. how can I achieve it, thanks.

  • do you need logged in user data ? – Aditya Shah Oct 24 '18 at 6:18
  • No , what i exactly need is customer who has ordering his product he ll fill shipping details, i need those details like firstname, lastname, address, zipcode etc. "Irrespective of their login " – ASHWINI MURTHY Oct 24 '18 at 6:22
  • Okay, on checkout page while filling billing and shipping details, right ? – Aditya Shah Oct 24 '18 at 6:25
  • yeah i need those information in my .phtml file. How can i do this? – ASHWINI MURTHY Oct 24 '18 at 6:28
  • on order success page ? – Mukesh Prajapati Oct 24 '18 at 6:29
2

Factory Method :

You need to inject \Magento\Customer\Model\CustomerFactory and \Magento\Customer\Model\AddressFactory in your construct

protected $_customerFactory;
protected $_addressFactory;

public function __construct(
    .............
    \Magento\Customer\Model\CustomerFactory $customerFactory,
    \Magento\Customer\Model\AddressFactory $addressFactory
    .............
)
{
    .............
    $this->_customerFactory = $customerFactory;
    $this->_addressFactory = $addressFactory;
    .............
}

then add this below code in your function

//get customer model before you can get its address data
$customer = $customerFactory->create()->load(1);    //insert customer id

//billing
$billingAddressId = $customer->getDefaultBilling();
$billingAddress = $this->_addressFactory->create()->load($billingAddressId);


//shipping
$shippingAddressId = $customer->getDefaultShipping();
$shippingAddress = $this->_addressFactory->create()->load($shippingAddressId);

Object Manager Method :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerID = 1;
$customer = $objectManager->create('Magento\Customer\Model\CustomerFactory')->load($customerID);
$addressFactory = $objectManager->create('Magento\Customer\Model\AddressFactory');

$billingAddressId = $customer->getDefaultBilling();
$billingAddress = $addressFactory->load($billingAddressId);

$shippingAddressId = $customer->getDefaultShipping();
$shippingAddress = $addressFactory->load($shippingAddressId);

Using Order ID :

$orderID = 100;

$orderObj = $objectManager->create('Magento\Sales\Model\Order')->load($orderID);

$shippingAddressObj = $orderObj->getShippingAddress();

$shippingAddressArray = $shippingAddressObj->getData();

$BillingAddressObj = $orderObj->getBillingAddress();

$BillingAddressArray = $BillingAddressObj->getData();

Get order ID :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$checkout_session = $objectManager->get('Magento\Checkout\Model\Session');
$order = $checkout_session->getLastRealOrder();
$orderID = $order->getIncrementId();

For better coding standard, Don't use Object Manager Directly.

  • Yes :) One should never use \Magento\Framework\App\ObjectManager::getInstance(). It defeats the purpose of dependency injection. +1 for explanation ! – Aditya Shah Oct 24 '18 at 6:40
  • here $orderId is hard-coded, can please help me to get the orderId dynamically? – ASHWINI MURTHY Oct 24 '18 at 6:52
  • Want to get by customer ? – Rohan Hapani Oct 24 '18 at 6:53
  • customer will be the guest user. so !!! – ASHWINI MURTHY Oct 24 '18 at 6:56
  • Is it useful for you? – Rohan Hapani Oct 24 '18 at 7:04
1

You can get this all by order id :

Try below code :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('Magento\Sales\Model\Order')->load($block->getOrderId()); // pass orderId
if($order->getCustomerId() === NULL){
    $firstname      = $order->getBillingAddress()->getFirstname();
    $lastname       = $order->getBillingAddress()->getLastname();
    $customer_name  = $firstname.' '.$lastname;
    $telephone_tmp  = $order->getBillingAddress()->getTelephone();
    $customer_email = $order->getBillingAddress()->getEmail();
    $postal         = $order->getBillingAddress()->getPostcode();
    $city           = $order->getBillingAddress()->getCity();
    $address_tmp    = $order->getBillingAddress()->getData('street');
    echo $customer_name."<-------- new customer";
}
/* getting Register Account Details */
else {
    $customer  = $objectManager->create('Magento\Customer\Model\Customer')->load($order->getCustomerId());
    //else, they're a normal registered user.
    $firstname = $customer->getDefaultBillingAddress()->getFirstname();
    $lastname  = $customer->getDefaultBillingAddress()->getLastname();
    echo $customer_name = $firstname.' '.$lastname ."<--------   exist customer";
}

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.