1

I want to fetch and show which customer bought which product and from where on front end. The issue is i am unable to fetch the above details through order increment id/order id.

Here is my front end templates

Here is my observer

So how can i get customer details on checkout_onepage_success page.

2 Answers 2

5

Well i would suggest to use Automatic Dependency injection but if you are using object manager then you can do it like this

<?php //NOT TESTED THOUGH
      --------------------
//Get Object Manager Instance
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($IncrementId); //Use increment id here.
$order->getCustomerName(); // Check that class. There are lot of functions for getting customers data. Though first you should check if any API available. Because thats a better solution.
7
  • Thanks @Kingshuk for giving solution, but i'm not using any api. Apr 3, 2017 at 10:42
  • 1
    No..not API..Magento said they can change the core code but not the APIs available in near future. If you go to app\code\Magento\Sales the first folder is API. Though you are good to go now. You can use the above code now i guess. But magento can change the core code anytime. Apr 3, 2017 at 10:53
  • Thanks @Kingshuk for your help. It's working. You made my day man. :) Apr 3, 2017 at 11:16
  • Great. Good to know @BikashRanjan Apr 3, 2017 at 11:19
  • up vote plz @Kingshuk Apr 3, 2017 at 13:40
1
<?php
// logined customer
 if ($order->getCustomerFirstname()) {
   $customerName = $order->getCustomerName();
 } else {
  // guest customer
   $billingAddress = $order->getBillingAddress();
   $customerName = $billingAddress->getFirstname() . ' ' . 
   $billingAddress->getLastname();
 }
echo $customerName;

?>

2
  • Please provide a commentary with your answer, to explain your approach and how it works. Sep 25, 2019 at 13:00
  • checking customer is guest or not. first name and Last name of the billing address is set as a customer name when the customer is guest.
    – T P T
    Sep 26, 2019 at 6:35

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.