How to get registered customer details in order success page(success.phtml) in M2.2?

I am using Magento 2.2. I want to display customer name, address line 1, adrdress line 2, city, phone, email in order success page.

This is my code in success.phtml:

    <?php /** @var $block \Magento\Checkout\Block\Onepage\Success */ ?>
<div class="checkout-success">
    <?php if ($block->getOrderId()):?>
        <?php if ($block->getCanViewOrder()) :?>
            <p><?= __('Your order number is: %1.', sprintf('<a href="%s" class="order-number"><strong>%s</strong></a>', $block->escapeHtml($block->getViewOrderUrl()), $block->escapeHtml($block->getOrderId()))) ?></p>
        <?php  else :?>
            <p><?= __('Your order # is: <span>%1</span>.', $block->escapeHtml($block->getOrderId())) ?></p>
        <?php endif;?>
            <p><?= /* @escapeNotVerified */ __('We\'ll email you an order confirmation with details and tracking info.') ?></p>
    <?php endif;?>

    <?= $block->getAdditionalInfoHtml() ?>

    <div class="actions-toolbar">
        <div class="primary">
            <a class="action primary continue" href="<?= /* @escapeNotVerified */ $block->getContinueUrl() ?>"><span><?= /* @escapeNotVerified */ __('Continue Shopping') ?></span></a>
        </div>
    </div>
</div>
<?php

$lid = $this->getOrderId();
echo  "Order ID:".$lid."<br/>";

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('Magento\Sales\Model\Order')->load($lid);


$totall = $order->getGrandTotal();
echo "Order Total:".$totall."<br/>";

$shippingAddress = $order->getShippingAddress();

if ($shippingAddress)
{
    $telephone_tmp  = $shippingAddress->getTelephone();
    $customer_email = $shippingAddress->getEmail();
    $postal         = $shippingAddress->getPostcode();
    $city           = $shippingAddress->getCity();
    $address_tmp    = $shippingAddress->getData('street');
}

/* getting Guest Account Details */
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";

}
$telephone = substr($telephone_tmp, 0, 10);

$space_count = (int)substr_count($address_tmp," ");
if ($space_count > 2)
{
    $address_pieces = explode(' ', $address_tmp);
    $address1       = $address_pieces[0].' '.$address_pieces[1].' '.$address_pieces[2];
    $address2       = $address_pieces[3].' '.$address_pieces[4];
}
else
{
    $address1 = $address_tmp;
}

echo "Name:".$customer_name."<br/>"; 

echo "Telephone No:".$telephone."<br/>"; 

echo "Email:".$customer_email."<br/>"; 

echo "postcode".$postal."<br/>"; 

echo "city".$city."<br/>"; 

echo "address1".$address1."<br/>";

echo "address2".$address2."<br/>";

Please help to do this.

Solutions

Try to use below code for registered and guest customers...

 $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
 $order         = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($block->getOrderId());

 $order->getCustomerEmail();

working code try to use it.

if this is helpful for you please give kudos.

Happy Coding!!

Try to use 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";
}

Working code.

For customer Information in order after order placed successfully you need to create Observer.

event :

<event name="checkout_onepage_controller_success_action">
        <observer name="magecomp_place_order_after" instance="Magecomp\Sms\Observer\Orderplaceafter" />
</event>

Create php file in Observer in your module Observer/Orderplaceafter.php

use Magento\Framework\Event\ObserverInterface;
class Orderplaceafter implements ObserverInterface
{
      protected $objectManager;
      public function __construct(
      \Magento\Framework\ObjectManagerInterface $objectManager
   )
   {
       $this->objectManager = $objectManager;
   }
   public function execute(\Magento\Framework\Event\Observer $observer)
   {
        $order_id = $observer->getData('order_ids');
        $order = $this->objectManager->create('Magento\Sales\Model\Order')->load($order_id[0]);
        $customerid = $order->getCustomerId();
        $customer = $this->objectManager->create('Magento\Customer\Model\Customer')->load($customerid);
         $customerName = $customer->getCustomerName()    
     return true;
   }
}

Similar questions

Magento2: How to get registered online customer only?
customer_list.phtml Result Expected result I want to show registered online customer only.
Onepage checkout: Already registered customer login redirects to create an account or login page
I have recently upgraded to 1.8.1 and am fairly new to magento. All other aspects of my site seems to be working just fine, however when a customer with an existing account adds item to the cart and goes to checkout, and then is presented with the one page section: ‘Already registered? Log in below:’ the login button redirects to the ‘Login or Crea...
Forgotten password page: Show information about registered customer e-mail addresses
When someone goes onto the forgotten password page and enters an email address once they click submit it reloads the log in page even if the email address doesn't exist on the database. Is there a way to get magento to check the database and if the password doesn't exist throw up an error message, and if the password does exist throw a success mess...
Magento 2 Get order details on success page?
I need to get the order information (shipping details, item SKU's) in order to send it over to the Amazon API for easy FBA shipping. Ideally I will be able to capture this information upon a successful payment. How do you get this information?
Magento 2 how can to get bank details in order success page
How can i get bank details in order success page when i select in bank payment page and checkout.
Magento2 rest api get customer details and cart details as a admin authorization
Magento2 rest api. If i am login as a admin user how can i get details of customer and it's cart items (items kept in cart for order later).

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.