3

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?

asked Sep 6, 2018 at 13:48

3 Answers 3

2

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])
answered Sep 6, 2018 at 14:00
8
  • Do i need to use setData if i store test and test2 into some variables? if i have like $line = 'test';$line2 = 'test'; Commented Sep 6, 2018 at 14:23
  • Check my updated answer. Commented Sep 6, 2018 at 14:32
  • Okay, i will check it, if line2 is empty does it works? Commented Sep 6, 2018 at 14:33
  • Yes then it will set blank value. Commented Sep 6, 2018 at 14:33
  • Nice, Its working great!!! Commented Sep 6, 2018 at 14:37
1

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.

answered Sep 6, 2018 at 14:08
1
  • i do not have any address_entity_id, have only customerId, using that i need to update if he has any address Commented Sep 6, 2018 at 14:25
1

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();
answered Mar 3, 2020 at 10:12
2
  • 1
    Its working good Commented Mar 3, 2020 at 10:51
  • 1
    Thanks for this code Commented Mar 3, 2020 at 10:52

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.