Magento 2 : Custom attribute of address not saving
-
12-04-2021 - |
Question
I am using following code and trying to save SetLatitude()
or ->setCustomAttribute('latitude',$address['latitude'])
but it's not working.
I followed this this solution
<?php
namespace ABC\Customerapi\Model;
use Magento\Framework\Exception\InputException;
class Customerapi implements \ABC\Customerapi\Api\CustomerapiInterface
{
CONST MAX_PASSWORD_LENGTH=12;
CONST MIN_PASSWORD_LENGTH=4;
private $customerFactory;
private $storeManager;
private $_request;
private $_tokenModelFactory;
protected $addressDataFactory;
protected $addressRepository;
# protected $customerRepository;
public function __construct(
\Magento\Framework\Webapi\Rest\Request $request,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Integration\Model\Oauth\TokenFactory $tokenModelFactory,
# \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
\Magento\Customer\Model\CustomerFactory $customerFactory,
\Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
\Magento\Customer\Api\Data\AddressInterfaceFactory $addressDataFactory
){
$this->_request = $request;
$this->storeManager = $storeManager;
$this->_tokenModelFactory = $tokenModelFactory;
$this->customerFactory = $customerFactory;
# $this->customerRepository = $customerRepository;
$this->addressRepository = $addressRepository;
$this->addressDataFactory = $addressDataFactory;
}
public function update($shop_name,$mobile,$email=null,$address,$customerId){
try{
$customer = $this->customerFactory->create();
$customer->getResource()->load($customer, $customerId);
$name_Array = $this->split_name($shop_name);
if($email){
$customer->setEmail($email);
}
$customer->setFirstname($name_Array[0]);
$customer->setMobile($mobile);
$customer->setLastname($name_Array[1]);
$customer->getResource()->save($customer);
if($address){
$data = ['block'=>$address['block']];
$addressModel = $this->addressDataFactory->create();
$addressModel->setFirstname($address['firstname'])
->setLastname($address['lastname'])
->setCountryId($address['country_id'])
->setRegionId($address['region_id'])
#->setRegion($address[''])
->setCity($address['city'])
->setPostcode($address['postcode'])
->setCustomerId($customerId)
->setStreet(array($address['street']))
->setTelephone($address['telephone'])
->setBlock($address['block']);
#->setData($data);
#->setCustomAttribute('preferred_calling',$address['preferred_calling'])
#->setCustomAttribute('latitude',$address['latitude'])
#->setCustomAttribute('longitude',$address['longitude']);
$this->addressRepository->save($addressModel);
}
$message = [
'status'=>200,
'id'=>$customerId,
'message'=>'account has been update successfully'
];
echo json_encode($message);
exit;
}catch(\Exeption $e){
throw new InputException(
__($e->getMessage())
);
}
}
}
Setup script:
$customerSetup -> addAttribute('customer_address',
'latitude',
[
'label' => 'Latitude',
'system' => 0,
'user_defined' => true,
'position' => 100,
'sort_order' =>100,
'visible' => false,
'required' => false,
'default_value' => '',
'note' => '',
'type' => 'varchar',
'input' => 'text',
]
);
$customerSetup -> getEavConfig() -> getAttribute('customer_address', 'latitude')->setData('is_user_defined',1)->setData('default_value','')-> setData('used_in_forms', ['adminhtml_customer_address', 'customer_register_address', 'customer_address_edit'])-> save();
Solution
please try this out to your setup script.
public function install(
ModuleDataSetupInterface $setup,
ModuleContextInterface $context
) {
$customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);
$customerAddressEntity = $customerSetup->getEavConfig()->getEntityType('customer_address');
$attributeSetId = $customerAddressEntity->getDefaultAttributeSetId();
/** @var $attributeSet AttributeSet */
$attributeSet = $this->attributeSetFactory->create();
$attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);
$customerSetup->addAttribute('customer_address', 'latitude', [
'label' => 'Latitude',
'input' => 'text',
'type' => 'varchar',
'required' => false,
'position' => 333,
'visible' => true,
'system' => false,
'is_used_in_grid' => false,
'is_visible_in_grid' => false,
'is_filterable_in_grid' => false,
'is_searchable_in_grid' => false,
'backend' => '',
'comment' => 'latitude'
]);
$attribute = $customerSetup->getEavConfig()->getAttribute('customer_address', 'latitude')
->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();
}
hope this will work for you!.
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange