2016-05-18 36 views
3

這是我第一個使用Magento 2的項目。我很難獲取登錄客戶的默認結算/送貨地址以顯示在我的前端自定義模塊。獲取登錄客戶的默認帳單/發貨地址Magento 2.0

到目前爲止,我有這樣的:

//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); 

但錯誤說: Call to undefined method Magento\Customer\Model\AddressFactory::load()

我想我接近,但我不知道下一步該怎麼做。提前致謝。

回答

5

正確的方式來獲得的記錄在客戶的帳單地址是:

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

它的工作!謝謝! :D – Ian

+2

直接使用對象管理器不是好習慣。 更重要的是,這裏應該使用適當的地址存儲庫。 –

+2

而不是指出「錯誤」的方式來做到這一點,然後只是留下爲什麼不寫一個真正支持如何實施的建議。 – LefterisL

相關問題