There must be something that I’m not seeing, some custom attribute I added to the customer_address_entity table isn’t not saved.
The idea is to from admin form add then (get/save) custom attribute into customer_address_entity
app/code/Mag/Ento/etc/db_schema.xml
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
<table name="customer_address_entity" resource="default">
<column xsi:type="int" name="mr_external_id" unsigned="true" nullable="true" comment="External id"/>
</table>
</schema>
app/code/Mag/Ento/etc/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="MagentoCustomerApiDataAddressInterface">
<attribute code="mr_external_id" type="int" />
</extension_attributes>
</config>
app/code/Mag/Ento/etc/di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="MagEntoApiDataCustomerAddressExtensionInterface" type="MagEntoModelCustomerAddressExtension"/>
<type name="MagentoCustomerApiAddressRepositoryInterface">
<plugin name="mag_ento_customer_address_extension_custom_attr_save"
type="MagEntoPluginCustomerAddressRepositoryPlugin"
sortOrder="10"/>
</type>
</config>
app/code/Mag/Ento/Api/Data/CustomerAddressExtensionInterface.php
namespace MagEntoApiData;
use MagentoFrameworkApiExtensionAttributesInterface;
interface CustomerAddressExtensionInterface extends ExtensionAttributesInterface
{
public const MR_EXTERNAL_ID = 'mr_external_id';
/**
* @return int|null
*/
public function getMrExternalId(): ?int;
/**
* @param int|null $value
* @return CustomerAddressExtensionInterface
*/
public function setMrExternalId(?int $value): CustomerAddressExtensionInterface;
}
app/code/Mag/Ento/Model/CustomerAddressExtension.php
namespace MagEntoModel;
use Mag/EntoApiDataCustomerAddressExtensionInterface;
use MagentoFrameworkModelAbstractExtensibleModel;
class CustomerAddressExtension extends AbstractExtensibleModel implements CustomerAddressExtensionInterface
{
/**
* Get mrExternalId
*
* @return int|null
*/
public function getMrExternalId(): ?int
{
$id = $this->getData(self::MR_EXTERNAL_ID);
return $id !== null ? (int) $id : null;
}
/**
* Set mrExternalId
*
* @param int|null $value
* @return CustomerAddressExtensionInterface
*/
public function setMrExternalId(?int $value): CustomerAddressExtensionInterface
{
$this->setData(self::MR_EXTERNAL_ID, $value);
return $this;
}
}
Now get/save the extension attribute
app/code/Mag/EntoPlugin/CustomerAddressRepositoryPlugin.php
namespace MagEntoPluginCompanyAddress;
use MagentoCustomerApiAddressRepositoryInterface;
use MagentoCustomerApiDataAddressInterface;
use MagentoCustomerApiDataAddressInterface as CustomerAddressInterface;
use MagentoFrameworkApiExtensionAttributesFactory;
class CustomerAddressRepositoryPlugin
{
/**
* @param ExtensionAttributesFactory $extensionAttributesFactory
* @param AddressRepositoryInterface $addressRepositoryInterface
*/
public function __construct(
private readonly ExtensionAttributesFactory $extensionAttributesFactory,
) {
}
public function afterGetById(AddressRepositoryInterface $subject, AddressInterface $address): AddressInterface
{
$extensionAttributes = $address->getExtensionAttributes();
if ($extensionAttributes === null) {
$extensionAttributes = $this->extensionAttributesFactory->create(CustomerAddressInterface::class);
}
$extensionAttributes->setMrExternalId(1234); // 1234 hard code value for test
$address->setExtensionAttributes($extensionAttributes);
return $address;
}
/**
* @param AddressRepositoryInterface $subject
* @param CustomerAddressInterface $address
* @return array
*/
public function beforeSave(AddressRepositoryInterface $subject, AddressInterface $address): array
{
$extensionAttributes = $address->getExtensionAttributes();
$extensionAttributes->setMrExternalId(1234); // 1234 hard code value for test
$address->setExtensionAttributes($extensionAttributes);
return [$address];
}
}
Not also working with afterSave
I trigger some save action in another file in order to insert rows in customer_address_entity
$address->setFirstname('john')
...
//also tried
$extensionAttributes->setMrExternalId('1234');
$address->setExtensionAttributes($extensionAttributes);
$this->customerAddressRepositoryInterface->save($address);
All the attributes are well saved in DB except mr_external_id attribute value.
nb:
What I also don’t understand is that in an observer save after, the value is available in the object but not saved in db.
[data_object (MagentoCustomerModelAddressInterceptor)] => Array
(
[parent_id] => 3193
[customer_id] => 3193
[street] => addr1
[extension_attributes] => Array
(
[mr_external_id] => 1234 //<------
)
...
)
I already implemented extension attributes but I don’t see what’s wrong in this case.