You have to implement extension_attribute for is_seller
attribute then you have to enable to add is_seller to the database.
First, read the article https://store.fooman.co.nz/blog/an-introduction-to-extension-attributes.html
First, you have to implement the service contract for your custom table to save the data.
Assume that
your data interface class is
StackExchange\ServiceContactTest\Api\Data\CustomInterface
And RepositoryInterface is StackExchange\ServiceContactTest\Api\CustomRepositoryInterface
First, you have to add is_seller
as extension_attribute for data interface Magento\Customer\Api\Data\CustomerInterface
.
Create extension_attributes.xml
at app/code/{Vendor}/{ModuleName}/etc
<?xml version="1.0"?>
<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\CustomerInterface">
<attribute code="is_seller" type="StackExchange\ServiceContactTest\Api\Data\CustomInterface" />
</extension_attributes>
</config>
After that Using plugin on Magento\Customer\Api\CustomerRepositoryInterface
, you need to save this custom table data.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Customer\Api\CustomerRepositoryInterface">
<plugin name="save_seller_table" type="StackExchange\ServiceContactTest\Plugin\OrderSave"/>
</type>
</config>
Plugin Class
<?php
namespace StackExchange\ServiceContactTest\Plugin;
use Magento\Framework\Exception\CouldNotSaveException;
class OrderSave
{
...
public function afterSave(
\Magento\Customer\Api\CustomerRepositoryInterface $subject,
\Magento\Customer\Api\Data\CustomerInterface $resultOrder
) {
$resultOrder = $this->saveFoomanAttribute($resultOrder);
return $resultOrder;
}
private function saveFoomanAttribute(\Magento\Customer\Api\Data\CustomerInterface $customer)
{
$extensionAttributes = $customer->getExtensionAttributes();
if (
null !== $extensionAttributes &&
null !== $extensionAttributes->getIsSeller()
) {
$foomanAttributeValue = $extensionAttributes->getIsSeller()->getValue();
try {
// The actual implementation of the repository is omitted
// but it is where you would save to the database (or any other persistent storage)
$this->foomanExampleRepository->save($customer->getEntityId(), $foomanAttributeValue);
} catch (\Exception $e) {
throw new CouldNotSaveException(
__('Could not add attribute to order: "%1"', $e->getMessage()),
$e
);
}
}
return $customer;
}