2

I've created a new customer address attribute called 'landline' (required). I've used the code generated by https://mage2gen.com/ which seems 'correct' to me:

  • Attribute is correctly created and displayed
  • I can save the attribute from the backoffice

However, if I try to create a new address programmatically and set a value for this new attribute, I get the following error:

"Landline" is a required value.

If I create the attribute but it's not required, then the address is generated, but the landline field is empty.

This is the code I use for address generation:

$address = $this->customerAddressFactory->create();

$address->setCustomerId($customerId)
                ->setFirstname($addressData['firstname'])
                ->setLastname($addressData['lastname'])
                ->setCountryId($addressData['country_id'])
                ->setRegionId($addressData['region_id'])
                ->setPostcode($addressData['postcode'])
                ->setCity($addressData['city'])
                ->setTelephone($addressData['fax'])
                ->setData('landline', $addressData['telephone'])
                ->setStreet($addressData['street_array']);

$this->customerAddressRepository->save($address);

As you can see, I'm using 'setData' for landline attribute. If I use: setLandline, the following error is thrown:

Call to undefined method Magento\Customer\Model\Data\Address::setLandline()

How can I save this attribute programmatically? What am I doing wrong? I'm using Magento 2.4.1

2

Please try using this

->setCustomAttribute('landline', $addressData['telephone']);

Happy coding

1
  • Thanks, that was it. I've looked for this answer in google and stackoverflow before asking and I didn't find anything. Hope this helps other people too! – PauGNU Oct 27 '20 at 11:32

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.