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?

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])
8
  • 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 Sep 6 '18 at 14:23
  • Check my updated answer. – Sukumar Gorai Sep 6 '18 at 14:32
  • Okay, i will check it, if line2 is empty does it works? – jafar pinjar Sep 6 '18 at 14:33
  • Yes then it will set blank value. – Sukumar Gorai Sep 6 '18 at 14:33
  • Nice, Its working great!!! – jafar pinjar Sep 6 '18 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.

1
  • i do not have any address_entity_id, have only customerId, using that i need to update if he has any address – jafar pinjar Sep 6 '18 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();
2
  • 1
    Its working good – user53909 Mar 3 '20 at 10:51
  • 1
    Thanks for this code – Savan Patel Mar 3 '20 at 10:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.