This is not a duplicate of this. I've referenced that question, but it didn't give me what I looking for.

I need to create a file with specific data on customer. I tried few methods I found on here, but no luck. I need to get information as follows,


I need to get these info using ObjectManager. I lnow it's not the best way, bou for now, I only able to use it.

Update I need to call these from external file, not as a module or a plugin. So I cannot use any block methods. Only possible way if for me is using the Object Manager directly. But the problem is all the methods I've tried failed.

This below is the code from the answer you guys said my question is duplicate of,

$customerId = $_POST["customer_id"];
$customer = $this->_customerRepository->getById($customerId);
$addresses = $customer->getAddresses(); 

but since I'm on outside PHP script, I cant use $this->_customerRepository or $customerRepository . I'm relatively new to Magento. So I'm not sure how to get things done most of the time.

  • @DineshYadav it's not. I just needed a way to get customer default address for external php script. I read that question and tried with it but wasn't successful
    – Joey
    Commented Apr 9, 2018 at 9:38
  • 1
    @Joey, you are trying to convince us this is not a duplicate. I could see why, but you are not giving us any details. If this is not a duplicate then I would vote to close your question because it is unclear. Please provide the code you already have so far and tell us what is the exact problem you have
    – 7ochem
    Commented Apr 9, 2018 at 10:43
  • 1
    @Joey, You can use customerRepository with the help of objectManager. Try this $customer = $objectManager->create('Magento\Customer\Api\CustomerRepositoryInterface')->getById($customerId); Commented Apr 9, 2018 at 12:01
  • 1
    In order to create an external script (in your m2 webroot) to use magento classes you have to do the next: use Magento\Framework\App\Bootstrap; require DIR . '/app/bootstrap.php'; $bootstrap = Bootstrap::create(BP, $_SERVER); $obj = $bootstrap->getObjectManager(); $state = $obj->get('Magento\Framework\App\State'); $state->setAreaCode('frontend'); $customer = $obj->get('Magento\Customer\Api\CustomerRepositoryInterface')->load($customerId); Commented Apr 9, 2018 at 12:09
  • 1
    @Joey, Your code is correct but you are printing the complete object which is very large. Use print_r($keyzz->__toArray()); It will display the customer address in array format. Commented Apr 10, 2018 at 4:28


Your Answer

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