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.

3 Answers 3

4

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.

4
  • Thank you. But it shows error " Fatal error: Uncaught Error: Call to a member function create() on null in ...." Commented Nov 22, 2017 at 9:55
  • check my updated answer @TamilSelvan Commented Nov 22, 2017 at 10:40
  • Still got error Fatal error: Uncaught Error: Call to a member function getFirstname() on boolean in. Please check my updated code @Chander Shekhar Commented Nov 22, 2017 at 10:58
  • I just removed getDefaultBillingAddress(), now its working.... @Chander Shekar. Commented Nov 22, 2017 at 12:32
3

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!!

3
  • hi @Rohit, using Objectmanager directly is not recommended. Commented Feb 22, 2019 at 10:19
  • Hi @jafarpinjar , you can use object manager directly in design files Commented May 9, 2019 at 11:07
  • yes @Rohit, we can use but not recommended, :) Commented May 9, 2019 at 11:39
2

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;
   }
}
1
  • Thank you for your suggestion. I just removed getDefaultBillingAddress(), now its working.... Commented Nov 22, 2017 at 12:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.