0

I want to add custom field "comment" to the order as an extension attribute. So, the first, I have added this column to following tables in database: "quote" and "sales_order".

It's my xml file:

<?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\Sales\Api\Data\OrderInterface">
        <attribute code="comment" type="string"/>
    </extension_attributes>
</config>

It's my observer ("sales_order_save_after"), where I'm trying to set "comment":

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use Magento\Sales\Api\OrderRepositoryInterface;

class SaveComment implements ObserverInterface
{
    protected $_rep;

    public function __construct(OrderRepositoryInterface $rep)
    {
        $this->_rep = $rep;
    }

    public function execute(Observer $observer)
    {
        $order = $observer->getEvent()->getData('order');
        $ext = $order->getExtensionAttributes();
        $ext->setComment('Hi, Magento!');
        $order->setExtensionAttributes($ext);
        $this->_rep->save($order);
    }
}

And it works, but doesn't add any attributes to database, so when I try to get it in another place with:

$comment = $order->getExtensionAttributes()->getComment();

I get null. Please, help me.

  • You must not add new columns to existing core table, you should create own tables instead. And in that case, you need to retrieve extension attributes from another table, as an example github.com/magento/magento2/blob/develop/app/code/Magento/Vault/… how to achieve it. – joni jones Sep 26 '17 at 7:33
  • Accept the answer If you find my answer helpful @Skorzhyk – Keyur Shah Oct 3 '17 at 18:49
  • Link in the comments is dead. Any chance on a new one? I keep seeing this link everywhere. – JNDPNT Dec 21 '17 at 10:00
  • Make sure to delete the generations folder. – numerical25 Jun 28 '18 at 22:50
4

You need to add the below plugin to di.xml

<type name="Magento\Sales\Api\OrderRepositoryInterface">
    <plugin name="get_order_comment" type="Vendor\Sample\Plugin\OrderGet"/>
</type>

Now add the plugin to add the data to the api

class OrderGet {

    protected $_orderExtensionFactory;

    public function __construct(
        \Magento\Sales\Api\Data\OrderExtensionFactory $orderExtensionFactory
    ) {
        $this->_orderExtensionFactory = $orderExtensionFactory;
    }

    public function afterGet(
        \Magento\Sales\Api\OrderRepositoryInterface $subject,
        \Magento\Sales\Api\Data\OrderInterface $order
    ) {
        $extensionAttributes = $order->getExtensionAttributes();
        $orderExtension = $extensionAttributes ? $extensionAttributes : $this->_orderExtensionFactory->create();

        $comment = {write the logic to get the comment. e.g. $order->getComment()}
        $orderExtension->setComment($comment);

        $order->setExtensionAttributes($orderExtension);

        return $order;
    }
}
0

I think you are missing some concept behind the extension attribute. To use extension attribute you need to pass your object to set and get data,

<?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\Sales\Api\Data\OrderInterface">
        <attribute code="comment" type="string" type="YOUR_VEDNOR\YOUR_MODULE\Api\Data\AttributeInterface"/>
    </extension_attributes>
</config>

Here you are missing type attribute. You need to pass your class here to set the attribute value. As per I know It is very long answer so I am adding very nice blog about of extension attribute

  • 1
    having 2 attributes with the same name is not allowed in XML – Philipp Sander Mar 7 '18 at 15:15
  • 1
    He does have a type. Its string . If he wants to store a basic number or text. Thats what he needs. You only use your method if you want it to be a object. – numerical25 Jun 28 '18 at 22:48
0

You can add extension attributes in Magento 2 Order data by creating aextension_attributes.xml file under etc folder.

Get insight into extension attributes in Magento 2 by referring blog,

Magento 2 Order Extension Attribute

add code,

<?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\Sales\Api\Data\OrderInterface">
        <attribute code="order_comment" type="string" />
    </extension_attributes>
</config>
  • How can we use order extension attribute with custom total(row) model ? – Kishan Patadia Jan 3 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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