-2

I have created a multi-select attribute(attribute_code is address_purpose) for the customer using UpgradeData

The options are created and the values are getting stored if I edit in magento admin.

But they are not updating once updated programmatically with addressId.

I have two Options Delivery, General.

So I am updating address using below code with the addressId.

  $addressPurpose = array("Delivery","General");
  $shippingAddressId = 15;
  $address = $this->_addressRepository->getById($shippingAddressId);  
  $address->setCustomerId(1);
  $address->setFirstname('test');
  $address->setLastname('test');
  $address->setCountryId('GB');
  $address->setPostcode('TW9 1AF');
  $address->setCity('Test');
  //$address->setData('address_purpose',$addressPurpose);
  //$address->setAddressPurpose($addressPurpose);
  $address->setCustomAttribute('address_purpose', $addressPurpose);
  $address->setTelephone('1213323213');

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

This code is not saving value for my custom attribute(address_purpose).

What is the problem here? Please, anyone support on this issue. Thanks

0

2 Answers 2

1

if you are saving multiselect attribute you have to store option id instead it's value

public function getAllOptions()
    {
        return [
           'option1' => [
            'label' => 'Delivery',
            'value' => 'delivery'
        ],
        'option2' => [
            'label' => 'General',
            'value' => 'general'
           ]
        ];
     }

$addressPurpose = array("option1","option2");
$address->setCustomAttribute('address_purpose', $addressPurpose);
$this->_addressRepository->save($address);
7
  • this will work if we have two option given, if only one option it doesn't work. example: $addressPurpose = array("option1"); Commented Feb 1, 2019 at 14:29
  • it is working for me for multiselect attribute Commented Feb 1, 2019 at 14:39
  • 1
    Provide like this and try, $addressPurpose = array("option1");, will it select only one item? Commented Feb 1, 2019 at 14:40
  • Yes @jafarpinjar it's strange but try for single in your case $address->setCustomAttribute('address_purpose', 'option1'); Commented Feb 1, 2019 at 14:40
  • if i provide option explicitly, it works, try to assign it to variable and save, does it work for you Commented Feb 1, 2019 at 14:43
0

$address = $this->_addressRepository->getById($shippingAddressId); vs. $this->addressRepository->save($address);?

Variable once with leading underscore and once without. I suppose that's only a typo in your posting. The rest of the code seems to be ok and should save the custom attribute in my opinion.

I would suggest to debug data in the save method in AddressRepository especially what happens with your address data before and after $addressModel->updateData($address);

6
  • sorry, its my mistake, but still value not getting saved.. Commented Feb 1, 2019 at 13:57
  • if you call $address->getCustomAttribute('address_purpose'); after $address->setCustomAttribute('address_purpose', $addressPurpose); does it return the correct value? If yes I would try to dump address data (original and changed object) to logfile in the mentioned method in my post. If not it might be a problem with your custom attribute.
    – HelgeB
    Commented Feb 1, 2019 at 14:04
  • Its giving me empty array.. Commented Feb 1, 2019 at 14:16
  • Looks like the custom attribute has not been created correctly. Does it have the correct entity type? entity_type_code should be customer_address and entity_type_id = 2. Please check in eav_attribute table : SELECT * FROM eav_attribute where attribute_code = 'address_purpose'
    – HelgeB
    Commented Feb 1, 2019 at 14:30
  • yes created correctly, if i save from admin end, its working perfectly Commented Feb 1, 2019 at 14:30

Your Answer

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

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