I am using below code to create customer address using customerId
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$addresss = $objectManager->get('\Magento\Customer\Model\AddressFactory');
$address = $addresss->create();
$address->setCustomerId(1256)
->setFirstname('test')
->setLastname('test')
->setCountryId('GB')
->setPostcode('NW p7j')
->setCity('test')
->setTelephone('1232323')
->setStreet('test')
->setIsDefaultBilling('1')
->setSaveInAddressBook('1');
try{
$address->save();
}catch (\Exception $e) {
Zend_Debug::dump($e->getMessage());
}
I have two lines for street, how can i set that? Right now street is storing with one line. also how can we update address if that customer already has any address?
3 Answers 3
Try the below code:
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$addresss = $objectManager->get('\Magento\Customer\Model\AddressFactory');
$address = $addresss->create();
$address->setCustomerId(3)
->setFirstname('test')
->setLastname('test')
->setCountryId('GB')
->setPostcode('NW p7j')
->setCity('test')
->setTelephone('1232323')
->setStreet(['test','test2'])
->setIsDefaultBilling('1')
->setSaveInAddressBook('1');
try{
$address->save();
}catch (\Exception $e) {
Zend_Debug::dump($e->getMessage());
}
?>
You need to pass array to street like my answer to save it in street, street1, street2 etc.
UPDATED ANSWER: If you have $line = 'test'; and $line2 = 'test' then try the below code for street
->setStreet([$line,$line2])
-
Do i need to use setData if i store test and test2 into some variables? if i have like $line = 'test';$line2 = 'test';Jafar Pinjar– Jafar Pinjar2018年09月06日 14:23:50 +00:00Commented Sep 6, 2018 at 14:23
-
Check my updated answer.Sukumar Gorai– Sukumar Gorai2018年09月06日 14:32:35 +00:00Commented Sep 6, 2018 at 14:32
-
Okay, i will check it, if line2 is empty does it works?Jafar Pinjar– Jafar Pinjar2018年09月06日 14:33:28 +00:00Commented Sep 6, 2018 at 14:33
-
Yes then it will set blank value.Sukumar Gorai– Sukumar Gorai2018年09月06日 14:33:46 +00:00Commented Sep 6, 2018 at 14:33
-
Nice, Its working great!!!Jafar Pinjar– Jafar Pinjar2018年09月06日 14:37:48 +00:00Commented Sep 6, 2018 at 14:37
If you look into the database structure of Magento 2, you will find that for a customer address entity, there is only one attribute for storing street.
Please look into the table eav_attribute and search for attribute code like %street%.
SELECT * FROM `eav_attribute` WHERE `attribute_code` LIKE '%street%'
You will find only one record having the attribute_code street.
Hence, to store the street value from two input boxes, first, you need to concatenate both values and then store the result for the attribute street.
Next, to update an already existing address, you need to write:
$address->setId(your_current_address_entity_id);
before $address->save();
Please let me know if you find any problem.
-
i do not have any address_entity_id, have only customerId, using that i need to update if he has any addressJafar Pinjar– Jafar Pinjar2018年09月06日 14:25:44 +00:00Commented Sep 6, 2018 at 14:25
I Did check and its working proper
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$addresss = $objectManager->get('\Magento\Customer\Model\AddressFactory');
$address = $addresss->create();
$address->setCustomerId($customerId)
->setFirstname($FirstName)
->setLastname($LastName)
->setCountryId($countryid)
->setPostcode($postcode)
->setCity($city)
->setTelephone($telephone)
->setFax($fax)
->setCompany($Company)
->setStreet($Street)
->setRegion($region)
->setSaveInAddressBook('1');
$address->save();
And if you want to set default billing & shipping address just add 2 line like below
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$addresss = $objectManager->get('\Magento\Customer\Model\AddressFactory');
$address = $addresss->create();
$address->setCustomerId($customerId)
->setFirstname($FirstName)
->setLastname($LastName)
->setCountryId($countryid)
->setPostcode($postcode)
->setCity($city)
->setTelephone($telephone)
->setFax($fax)
->setCompany($Company)
->setStreet($Street)
->setRegion($region)
->setIsDefaultBilling('1')
->setIsDefaultShipping('1')
->setSaveInAddressBook('1');
$address->save();
-
1Its working gooduser53909– user539092020年03月03日 10:51:55 +00:00Commented Mar 3, 2020 at 10:51
-
1Thanks for this codeSavan Patel– Savan Patel2020年03月03日 10:52:27 +00:00Commented Mar 3, 2020 at 10:52