Как получить адрес клиента по идентификатору клиента?


Как получить адрес клиента/платежный адрес по идентификатору клиента? вот что я сделал до сих пор:

$customerId = $_POST["customer_id"];
$customer = $this->_customerRepository->getById($customerId);
$address = $this->_addressRepository->getByCustomerId($customerId);//error
Author: Rafael Corrêa Gomes, 2017-02-14

4 answers

Вы не можете получить адрес на основе идентификатора клиента, поэтому этот код никогда не будет работать:

$address = $this->_addressRepository->getByCustomerId($customerId);//error

Потому что метод getByCustomerId не существует в классах контрактов на обслуживание.

Что вы можете сделать, так это использовать класс клиента контракта на обслуживание данных со следующим кодом:

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

Пожалуйста, обратите внимание, что getAddresses вернет массив Magento\Customer\Api\Data\AddressInterface.

Если вам нужен платежный адрес по умолчанию, вы можете позвонить:

$billingAddress = $customer->getDefaultBilling(); 
 11
Author: Raphael at Digital Pianism, 2017-02-14 08:55:17

Для получения аддера клиента с использованием идентификатора заказа в файле .phtml

$customerId = 3;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerObj = $objectManager->create('Magento\Customer\Model\Customer')->load($customerId);
$customerAddress = array();

foreach ($customerObj->getAddresses() as $address)
{
    $customerAddress[] = $address->toArray();
}

foreach ($customerAddress as $customerAddres) {

    echo $customerAddres['street'];
    echo $customerAddres['city'];
}
 7
Author: Abhinav Singh, 2017-06-23 07:10:20
protected $_address;

public function __construct(
    ...
    \Magento\Customer\Model\Address $address,
    ...     
)
{
    ...
    $this->_address = $address;
    ...
}

Используйте в своей функции:-

$addressObj = $this->_address;
$addressObj->load($addressid); 

Вы можете получить коллекцию адресов, как показано ниже: -

$addresses = $addressObj->getCollection();
 2
Author: Purushotam Sharma, 2017-12-28 06:18:30

$частный $customerfactory;

public function __construct(
    \Magento\Customer\Model\CustomerFactory $customerFactory,
    Context $context
) {
    parent::__construct($context);
    $this->customerFactory = $customerFactory;
}

public function execute()
{
    $customerId = 1;
    $customer = $this->customerFactory->create();
    echo "<pre>";
    var_dump($customer->getAddressCollection()->addFieldToFilter('parent_id',$customerId)->getData());
}
 0
Author: bashid mohd, 2020-02-18 06:28:27