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)

0

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);
}
0

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();
    }
}
0

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

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.