Answers:
您无法根据客户ID检索地址,因此该代码将永远无法使用:
$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文件中的订单ID获取客户地址
$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();
$ private $ 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());
}
$customer->getDefaultBilling();
它时返回NULL