Setting an extension attribute for customer address won't assign it to object so it's not validated and saved to database correctly.
1. Setup extension attribute:
_extension_attributes.xml_
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
<extension_attributes for="Magento\Customer\Api\Data\AddressInterface">
<attribute code="foo" type="string"/>
</extension_attributes>
</config>
2. Save extension attribute
<?php
use Magento\Customer\Api\AddressRepositoryInterface;
use Magento\Customer\Api\Data\AddressInterface;
use Magento\Customer\Api\Data\AddressInterfaceFactory;
use Magento\Framework\Api\ExtensionAttributesFactory;
class Test
{
private $addressInterfaceFactory;
private $addressRepository;
private $extensionFactory;
public function __construct(
AddressInterfaceFactory $addressInterfaceFactory,
AddressRepositoryInterface $addressRepository,
ExtensionAttributesFactory $extensionFactory
) {
$this->addressInterfaceFactory = $addressInterfaceFactory;
$this->addressRepository = $addressRepository;
$this->extensionFactory = $extensionFactory;
}
public function test()
{
$address = $this->addressInterfaceFactory->create();
// save required address fields
/** @var \Magento\Customer\Api\Data\AddressExtensionInterface $extension */
$extension = $this->extensionFactory->create(AddressInterface::class);
$extension->setFoo('bar');
$address->setExtensionAttributes($extension);
$this->addressRepository->save($address);
}
}
At this point of address save in the repository I expect to see foo
field set in the $addressModel
object data. Further it would be validated and saved to DB.
Field is not set, object still has it in extension_attributes
:
["extension_attributes"]=>
array(1) {
["foo"]=>
string(3) "bar"
}
This seems to happen because DataObjectProcessor
is used inside of updateData
method.
DataObjectProcessor
does not replace the key while parsing extension attributes.
@dankocherga thank you for your feedback.
Customer custom address attributes are available in Enterprise edition only.
As you use Community Edition please address your question at programming questions forum or Magento Stack Exchange. As it is not a native behavior of Magento CE and we can not treat it as an issue
@veloraven, extension attributes and EAV attributes aren't the same thing, Are you sure you're not mixing up those?
OK, closing the issue since it's not going to be fixed by the core team.
I have used custom attribute method to solve the problem:
<?php
$customerAddress->setCustomAttribute('attribute_code', 42);
@dankocherga can you share what did you do ? I'm trying to do the same. Thanks