Today we discuss how in Magento 2 get customer address. Which include the default shipping address and default billing address also the all customer address list too. There are two types of addresses related to customer shipping and billing. you can add the unlimited address as a customer. let start with our example.
Customer Default Billing Address
Get the customer defaut billing address.
namespace QaisarSatti/HelloWorld/Block;
use Magento\Backend\App\Action;
class Delete extends Action
{
protected $customerFactory;
protected $addressFactory;
public function __construct(
\Magento\Customer\Model\CustomerFactory $customerFactory,
\Magento\Customer\Model\AddressFactory $addressFactory
)
{
$this->customerFactory = $customerFactory;
$this->addressFactory = $addressFactory;
}
public function getCustomerDefaultBillingAddress()
{
$customerObject = $this->customerFactory->create()->load(10);
$billingAddressId = $customerObject->getDefaultBilling();
return $billingAddress = $this->addressFactory->create()->load($billingAddressId);
}
}
use Magento\Backend\App\Action;
class Delete extends Action
{
protected $customerFactory;
protected $addressFactory;
public function __construct(
\Magento\Customer\Model\CustomerFactory $customerFactory,
\Magento\Customer\Model\AddressFactory $addressFactory
)
{
$this->customerFactory = $customerFactory;
$this->addressFactory = $addressFactory;
}
public function getCustomerDefaultBillingAddress()
{
$customerObject = $this->customerFactory->create()->load(10);
$billingAddressId = $customerObject->getDefaultBilling();
return $billingAddress = $this->addressFactory->create()->load($billingAddressId);
}
}
Customer Default Shipping Address
Get the customer defaut billing address.
namespace QaisarSatti/HelloWorld/Block;
use Magento\Backend\App\Action;
class Delete extends Action
{
protected $customerFactory;
protected $addressFactory;
public function __construct(
\Magento\Customer\Model\CustomerFactory $customerFactory,
\Magento\Customer\Model\AddressFactory $addressFactory
)
{
$this->customerFactory = $customerFactory;
$this->addressFactory = $addressFactory;
}
public function getCustomerDefaultBillingAddress()
{
$customerObject = $this->customerFactory->create()->load(10);
$shippingAddressId = $customerObject->getDefaultShipping();
return $shippingAddress = $this->addressFactory->create()->load($shippingAddressId);
}
}
use Magento\Backend\App\Action;
class Delete extends Action
{
protected $customerFactory;
protected $addressFactory;
public function __construct(
\Magento\Customer\Model\CustomerFactory $customerFactory,
\Magento\Customer\Model\AddressFactory $addressFactory
)
{
$this->customerFactory = $customerFactory;
$this->addressFactory = $addressFactory;
}
public function getCustomerDefaultBillingAddress()
{
$customerObject = $this->customerFactory->create()->load(10);
$shippingAddressId = $customerObject->getDefaultShipping();
return $shippingAddress = $this->addressFactory->create()->load($shippingAddressId);
}
}
Customer All Address
Get the customer all address.
namespace QaisarSatti/HelloWorld/Block;
use Magento\Backend\App\Action;
class Delete extends Action
{
protected $customerFactory;
protected $addressFactory;
public function __construct(
\Magento\Customer\Model\CustomerFactory $customerFactory,
\Magento\Customer\Model\AddressFactory $addressFactory
)
{
$this->customerFactory = $customerFactory;
$this->addressFactory = $addressFactory;
}
public function getCustomerDefaultBillingAddress()
{
$customerObject = $this->customerFactory->create()->load(10);
return $customerObject->getAddresses();
}
}
use Magento\Backend\App\Action;
class Delete extends Action
{
protected $customerFactory;
protected $addressFactory;
public function __construct(
\Magento\Customer\Model\CustomerFactory $customerFactory,
\Magento\Customer\Model\AddressFactory $addressFactory
)
{
$this->customerFactory = $customerFactory;
$this->addressFactory = $addressFactory;
}
public function getCustomerDefaultBillingAddress()
{
$customerObject = $this->customerFactory->create()->load(10);
return $customerObject->getAddresses();
}
}