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!
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!
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