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.
Best Answer
Try to use below code:
Working code.