cancel
Showing results for 
Search instead for 
Did you mean: 

How to get

SOLVED

How to get

1_default_address_1.jpg

1 ACCEPTED SOLUTION

Accepted Solutions

Re: How to get

Hi @maratheprash 

Although Magento 2 enables customers to have many addresses stored in an address book, only one can be a default billing and one – default shipping address. And it is quite easy to get or set billing or shipping address by a customer object.
 

The sample code below leverages customerFactory to get a customer object. Besides, it utilizes addressFactory to get an address by its ID. Thus, it is possible to get a default billing or shipping address ID from a customer by calling its getDefaultBilling() or getDefaultShipping() functions.

class SetCustomer
{
    protected $_customerFactory;
    protected $_addressFactory;
 
    public function __construct(\Magento\Customer\Model\CustomerFactory $customerFactory,
                                \Magento\Customer\Model\AddressFactory $addressFactory
    )
    {
        $this->_customerFactory = $customerFactory;
        $this->_addressFactory = $addressFactory;
    }
 
    //get customer model before you can get its address data
    $customer = $customerFactory->create()->load(1);    //insert customer id
 
    //billing
    $billingAddressId = $customer->getDefaultBilling();
    $billingAddress = $this->_addressFactory->create()->load($billingAddressId);
    //now you can update your address here and don't forget to save
    $billingAddress->save();

    //shipping
    $shippingAddressId = $customer->getDefaultShipping();
    $shippingAddress = $this->_addressFactory->create()->load($shippingAddressId);
    //now you can update your address here and don't forget to save
    $shippingAddress->save();


for more info :
magento-2-get-default-billing-and-shipping-address-by-customer.html 
https://magento.stackexchange.com/a/187779 

I hope it will help you!

 

View solution in original post

2 REPLIES 2

Re: How to get

Hi @maratheprash 

Although Magento 2 enables customers to have many addresses stored in an address book, only one can be a default billing and one – default shipping address. And it is quite easy to get or set billing or shipping address by a customer object.
 

The sample code below leverages customerFactory to get a customer object. Besides, it utilizes addressFactory to get an address by its ID. Thus, it is possible to get a default billing or shipping address ID from a customer by calling its getDefaultBilling() or getDefaultShipping() functions.

class SetCustomer
{
    protected $_customerFactory;
    protected $_addressFactory;
 
    public function __construct(\Magento\Customer\Model\CustomerFactory $customerFactory,
                                \Magento\Customer\Model\AddressFactory $addressFactory
    )
    {
        $this->_customerFactory = $customerFactory;
        $this->_addressFactory = $addressFactory;
    }
 
    //get customer model before you can get its address data
    $customer = $customerFactory->create()->load(1);    //insert customer id
 
    //billing
    $billingAddressId = $customer->getDefaultBilling();
    $billingAddress = $this->_addressFactory->create()->load($billingAddressId);
    //now you can update your address here and don't forget to save
    $billingAddress->save();

    //shipping
    $shippingAddressId = $customer->getDefaultShipping();
    $shippingAddress = $this->_addressFactory->create()->load($shippingAddressId);
    //now you can update your address here and don't forget to save
    $shippingAddress->save();


for more info :
magento-2-get-default-billing-and-shipping-address-by-customer.html 
https://magento.stackexchange.com/a/187779 

I hope it will help you!

 

Re: How to get

Thank you so much ! 

Can you please guide a little bit about this ? 

https://community.magento.com/t5/Magento-2-x-Technical-Issues/How-to-change-quot-shippingaddressfrom...p/450128#M14257