2

I need to save the extension attribute for Order item. I have tried using plugin with below code, but it is not working for me. I have placed a log inside the method but the method is not working.

<type name="Magento\Sales\Api\OrderItemRepositoryInterface">
 <plugin name="order_item_extension_attr" type="Vendor\CustomModule\Plugin\Model\Test"/>
</type>
public function afterGet(
 \Magento\Sales\Api\OrderItemRepositoryInterface $orderItem, 
 \Magento\Sales\Api\Data\OrderItemExtensionInterface $extension = null
 ) {
}

How to save and get order item extension attribute?

Edited: Used an observer after order saved, stored the order and item extension attributes in db. Which is working fine, but showing the order item values are not showing up in the order view page.

asked Aug 2, 2020 at 17:35

3 Answers 3

2

1. If you want to use extension attribute then you must declare extension attribute at app/code/YOurModule/{Modulename}/etc/extension_attributes.xml.

Create extension_attributes.xml

<?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\OrderItemInterface">
 <attribute code="{fieldname}" type="string" />
 </extension_attributes>
</config>

Assume that field type is varchar/text.

2. Create after plugin on save() method of Magento\Sales\Api\OrderRepositoryInterface while save Order using Magento\Sales\Api\OrderRepositoryInterface:save method.

answered Aug 3, 2020 at 5:02
1
  • Hi @Amit Thank you for your response. In database the extension attributes are saving correctly but when view the order, the order item extension attributes are not showing the values. Please adivse. Commented Aug 3, 2020 at 5:37
0

When you retrieve orders' items, you are indirectly calling the getList method of OrderItemRepositoryInterface. That's where you have to put your plugin.

Plugin declaration in di.xml

 <type name="Magento\Sales\Api\OrderItemRepositoryInterface">
 <plugin name="vendor_module_order_item_extension_attribute" type="Vendor\Module\Plugin\OrderItem\OrderItemRepository" />
 </type>

The afterGetList plugin method, where you provide the actual extension attributes data (in my case in a extra_data container)

 /**
 * @param OrderItemRepositoryInterface $subject
 * @param OrderItemSearchResultInterface $searchResult
 * @return OrderItemSearchResultInterface
 */
 public function afterGetList(
 OrderItemRepositoryInterface $subject,
 OrderItemSearchResultInterface $searchResult
 ): OrderItemSearchResultInterface
 {
 $orderItems = $searchResult->getItems();
 foreach ($orderItems as &$item) {
 $extensionAttributes = $this->myOrderItemExtensionFactory->create();
 $this->myOrderItemExtensionResource->load($extensionAttributes, $item->getItemId(), 'entity_id');
 $item->getExtensionAttributes()->setExtraData($extensionAttributes);
 }
 return $searchResult;
 }
answered Apr 7, 2022 at 12:44
1
  • $this->myOrderItemExtensionResource ? Commented Sep 15, 2022 at 12:34
-1

add below plugin to your di.xml

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

Now create below plugin here

Vendor\Module\Plugin\Yourplugin

class Yourplugin {
 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;
 }
}
answered Aug 3, 2020 at 4:24
1
  • thank you for your response. But I have asked for order item level only, for order its working fine. Commented Aug 3, 2020 at 5:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.