2

I have created one custom field in Magento 2 payment table through install schema and want to add getter and setter to the Magento\Quote\Api\Data\PaymentInterface to set and add data.

extension_attributes.xml

<extension_attributes for="Magento\Quote\Api\Data\PaymentInterface">
    <attribute code="policy_no" type="string"/>
</extension_attributes>

When i want to retrieve that value through below code i am getting null.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $paymentInformation = $objectManager->create('Magento\Quote\Api\Data\PaymentInterface');
        $extAttributes = $paymentInformation->getExtensionAttributes();
        var_dump($extAttributes);exit;

1 Answer 1

0

You have to setExtensionAttributes manually.

Example:

CustomerRepositoryInterfacePlugin.php

 use Magento\Customer\Api\CustomerRepositoryInterface;
 use Magento\Customer\Api\Data\CustomerExtensionFactory;


class CustomerRepositoryInterfacePlugin
{

[...]
public function __construct(\Psr\Log\LoggerInterface $logger,
                            CustomerExtensionFactory $customerExtensionFactory)
{
    $this->logger = $logger;
    $this->customerExtensionFactory = $customerExtensionFactory;
}

public function afterGetById(CustomerRepositoryInterface $subject,    \Magento\Customer\Api\Data\CustomerInterface $result)
 {
    $extension = $result->getExtensionAttributes() ? $result->getExtensionAttributes() : $this->customerExtensionFactory->create();

    $extension->setCustomAttribute("Custom Attribute");

    $result->setExtensionAttributes($extension);
    return $result;
}
}

di.xml

<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="Namespace-Module" type="Namespace\Module\Plugin\CustomerRepositoryInterfacePlugin" sortOrder="5"/>
     </type>
</config>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.