获取登录客户Magento 2.0的默认结算/送货地址

Ian*_*Ian 3 php frontend magento magento2

这是我使用Magento 2的第一个项目.我很难获得登录客户的默认账单/送货地址,以显示在我的自定义模块的前端.

到目前为止我有这个:

//this gets the billing id which is an integer. I'm thinking it must be loaded to get the whole data of the address
$billingId =  $customerSession->getCustomer()->getDefaultBilling();

//just found this in the internet and thought it might be the same as loading an order, but it doesn't work
$address = $objectManager->create('\Magento\Customer\Model\AddressFactory')->load($billingId);
Run Code Online (Sandbox Code Playgroud)

但错误说: Call to undefined method Magento\Customer\Model\AddressFactory::load()

我想我很接近,但我不知道接下来该做什么.提前致谢.

Man*_*rla 5

获取登录客户的帐单邮寄地址的正确方法是:

$billingID =  $customerSession->getCustomer()->getDefaultBilling();
$address = $objectManager->create('Magento\Customer\Model\Address')->load($billingID);
echo "<pre>";print_r($address->getData());exit;
Run Code Online (Sandbox Code Playgroud)

  • 直接使用对象管理器不是一个好习惯.更重要的是,这里应该是使用地址的适当存储库. (5认同)
  • 而不是指出"错误"的方式,然后只是留下为什么不写一个如何真正被实施的建议. (4认同)