5

I'm using Magento v2.1.6 and I have created a custom attribute for customer address like this:

$customerEntity = $customerSetup->getEavConfig()->getEntityType('customer_address');
$attributeSetId = $customerEntity->getDefaultAttributeSetId();

$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

$customerSetup->addAttribute('customer_address', 'subdistrict', [
                'type' => 'varchar',
                'label' => 'District',
                'input' => 'text',
                'required' => false,
                'visible' => true,
                'visible_on_front' => true,
                'user_defined' => false,
                'sort_order' => 83,
                'position' => 83,
                'system' => 0,
]);

$attribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'subdistrict')
                ->addData([
                    'attribute_set_id' => $attributeSetId,
                    'attribute_group_id' => $attributeGroupId,
                    'used_in_forms' => ['adminhtml_customer_address', 'customer_address_edit', 'customer_register_address','customer_address']
                ]);
$attribute->save();

I tried to save/update in one of the customer address like this:

public function __construct( 
  \Magento\Customer\Api\AddressRepositoryInterface $addressRepository
)
{ 
   $this->addressRepository = $addressRepository;
}

public function execute(){
 $addressId = 1;
 $subdistrict = "Harbor City";
 $address = $this->addressRepository->getById($addressId);
 $address->setCustomAttribute('subdistrict',$subdistrict); 
 $this->addressRepository->save($address);
}

But I am always getting an error saying :

{"0":"Class object does not exist","1":"#0 /var/www/Project/lib/internal/Magento/Framework/Reflection/MethodsMap.php(155): ReflectionClass->__construct('object')\n#1 /var/www/Project/lib/internal/Magento/Framework/Reflection/MethodsMap.php(106)

3 Answers 3

1

You need to pass \Magento\Customer\Api\Data\AddressInterface $address object for save customer address attribute value.

<?php
public function __construct(        
    \Magento\Framework\App\Action\Context $context,
    \Magento\Customer\Api\AddressRepositoryInterface $addressRepository,      
    \Magento\Customer\Api\Data\AddressInterfaceFactory $addressDataFactory
)
{ 
   $this->addressRepository = $addressRepository;
   $this->addressDataFactory = $addressDataFactory;
   parent::__construct($context);
}

protected function _extractAddress()
{
    $customerId = YOUR_CUSTOMER_ID; //5

    $addressDataObject = $this->addressDataFactory->create();        
    $addressDataObject->setCustomerId($customerId)
         ->setCustomAttribute('subdistrict',$subdistrict)
         ->setIsDefaultBilling(1)
         ->setIsDefaultShipping(1);
    return $addressDataObject;
}
/**
  * Save customer address attribute value
  */
public function execute()
{
    try {
        $address = $this->_extractAddress();
        $this->_addressRepository->save($address);
        return true;
    } catch (\Exception $e) {
        return $e->getMessage();
    }
}
4
  • Please let me know if you have any issue. Commented May 28, 2018 at 5:40
  • This does not work because of missing required fields Commented Oct 2, 2018 at 14:13
  • @RakeshJesadiya, I have created multi-select attribute successfully, value not getting saved from admin. magento.stackexchange.com/questions/260079/… Commented Feb 1, 2019 at 7:55
  • 1
    ->setCustomAttribute('subdistrict',$subdistrict) works
    – Rushvi
    Commented Sep 20, 2022 at 21:34
1

For show it on address/edit.phtml like :

<?=$block->getAddress()->getShippingInfo();?>

Add in your di.xml

<preference for="Magento\Customer\Model\Data\Address" type="vendor\CustomerAccount\Model\Data\Address" />

And in your model :

<?php


namespace vendor\CustomerAccount\Model\Data;

class Address extends \Magento\Customer\Model\Data\Address
{
    /**
     * Get shipping_info
     *
     * @return string|null
     */
    public function getShippingInfo()
    {
        return $this->_get('shipping_info');
    }
}

The save is automatic if you have created it normaly

-1

You need to used code like this.

protected $customerFactory;
protected $customerData;
protected $customer;

public function __construct(
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        \Magento\Customer\Model\Customer $customer,
        \Magento\Customer\Model\Data\Customer $customerData)
    {
        $this->customerFactory  = $customerFactory;
        $this->customer = $customer;
        $this->customerData = $customerData;
    }

public function execute()
{
   $customerId = YOUR_CUSTOMER_ID;
   $subdistrict = "Downtown";
   $customer = $this->customerFactory->create();
   $this->customerData = $this->customer->getDataModel();
   $this->customerData->setId($customerId);
   $this->customerData->setCustomAttribute('subdistrict', $subdistrict);
   $this->customer->updateData($this->customerData);
}
3
  • 1
    @DhirenVason its for customer address attribute not customer attribute Commented May 20, 2018 at 8:15
  • @IdhamChoudry can you tell, in your above code, for what you have create attribute? Commented May 21, 2018 at 3:19
  • @DhirenVasoya, Here i am facing same issue, can you look inti it please, magento.stackexchange.com/questions/260079/… Commented Feb 1, 2019 at 7:56

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.