Get Customer Address with customer ID programmatically Magento2
<?php /** * * Copyright © 2015 Bluethinkcommerce. All rights reserved. */ namespace Module\Api\Controller\Index; class Customeraddress extends \Magento\Framework\App\Action\Action { /** * @var \Magento\Framework\App\Cache\TypeListInterface */ protected $_cacheTypeList; /** * @var \Magento\Framework\App\Cache\StateInterface */ protected $_cacheState; /** * @var \Magento\Framework\App\Cache\Frontend\Pool */ protected $_cacheFrontendPool; /** * @var \Magento\Framework\View\Result\PageFactory */ protected $resultPageFactory; /** * @param Action\Context $context * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList * @param \Magento\Framework\App\Cache\StateInterface $cacheState * @param \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory */ protected $customerRepository; protected $addressRepository; public function __construct( \Magento\Framework\App\Action\Context $context, \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList, \Magento\Framework\App\Cache\StateInterface $cacheState, \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool, \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository, \Magento\Customer\Api\AddressRepositoryInterface $addressRepository, \Magento\Framework\View\Result\PageFactory $resultPageFactory ) { parent::__construct($context); $this->_cacheTypeList = $cacheTypeList; $this->_cacheState = $cacheState; $this->_cacheFrontendPool = $cacheFrontendPool; $this->resultPageFactory = $resultPageFactory; $this->customerRepository = $customerRepository; $this->addressRepository = $addressRepository; } /** * Flush cache storage * */ public function execute() { $customer_id=$this->getRequest()->getParam('custid'); //print_r($customer_id) ; $customerId = $customer_id; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $customerFactory=$objectManager->get('Magento\Customer\Model\CustomerFactory'); $addressFactory=$objectManager->get('Magento\Customer\Model\AddressFactory'); $address=$objectManager->get('Magento\Customer\Model\Address'); $addrcoll= $objectManager->create('\Magento\Customer\Model\Address')->getCollection(); /* echo "<pre>"; print_r($addrcoll->getData()); exit();*/ $customerObj = $objectManager->create('Magento\Customer\Model\Customer')->load($customerId); $customerAddress = array(); foreach ($customerObj->getAddresses() as $address) { $customerAddress[] = $address->toArray(); } $main=array(); $i=0; foreach ($customerAddress as $customerAddres) { if ($i<1) { $add['customer_id']= $customerAddres['parent_id']; $add['street']= $customerAddres['street']; $add['city']=$customerAddres['city']; $add['postcode']=$customerAddres['postcode']; $customerAddres['country_id']; $add['country_name']=$customerAddres['Country_name']=$objectManager->create('\Magento\Directory\Model\Country')->load($customerAddres['country_id'])->getName(); $main[]=$add; } $i++; } /* echo "<pre>"; print_r(json_encode($main));*/ echo "<pre>"; echo "{"."<br>".json_encode($main,JSON_PRETTY_PRINT)."<br>"."}"; } }
Comments
Post a Comment