Как получить адрес клиента по идентификатору клиента?
Как получить адрес клиента/платежный адрес по идентификатору клиента? вот что я сделал до сих пор:
$customerId = $_POST["customer_id"];
$customer = $this->_customerRepository->getById($customerId);
$address = $this->_addressRepository->getByCustomerId($customerId);//error
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();
Для получения аддера клиента с использованием идентификатора заказа в файле .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'];
}
protected $_address;
public function __construct(
...
\Magento\Customer\Model\Address $address,
...
)
{
...
$this->_address = $address;
...
}
Используйте в своей функции:-
$addressObj = $this->_address;
$addressObj->load($addressid);
Вы можете получить коллекцию адресов, как показано ниже: -
$addresses = $addressObj->getCollection();
$частный $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());
}