0

Error: Call to undefined method Magento\Sales\Api\Data\OrderItemExtension::setGuid()

di.xml

<?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\Sales\Api\OrderItemRepositoryInterface">
        <plugin name="guid_extension_attribute" type="Convert\GUID\Plugin\OrderItemRepositoryPlugin" />
     </type>
</config>

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\OrderInterface">
        <attribute code="guid" type="string"/>
    </extension_attributes>
</config>

OrderItemRepositoryPlugin.php

<?php

namespace Convert\GUID\Plugin;

use Magento\Sales\Api\Data\OrderItemExtensionFactory;
use Magento\Sales\Api\Data\OrderItemExtensionInterface;
use Magento\Sales\Api\Data\OrderItemInterface;
use Magento\Sales\Api\Data\OrderItemSearchResultInterface;
use Magento\Sales\Api\OrderItemRepositoryInterface;

/**
 * Class OrderItemRepositoryPlugin
 */
class OrderItemRepositoryPlugin
{
    /**
     * Order feedback field name
     */
    const FIELD_NAME = 'guid';

    /**
     * Order Extension Attributes Factory
     *
     * @var OrderItemExtensionFactory
     */
    protected $extensionFactory;

    /**
     * OrderItemRepositoryPlugin constructor
     *
     * @param OrderItemExtensionFactory $extensionFactory
     */
    public function __construct(OrderItemExtensionFactory $extensionFactory)
    {
        $this->extensionFactory = $extensionFactory;
    }

    /**
     *
     * @param OrderItemRepositoryInterface $subject
     * @param OrderItemInterface $orderItem
     *
     * @return OrderItemInterface
     */
    public function afterGet(OrderItemRepositoryInterface $subject, OrderItemInterface $orderItem)
    {
        $guid = $orderItem->getData(self::FIELD_NAME);
        $extensionAttributes = $orderItem->getExtensionAttributes();
        $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();
        $extensionAttributes->setGuid($guid);
        $orderItem->setExtensionAttributes($extensionAttributes);

        return $orderItem;
    }

    /**
     *
     * @param OrderItemRepositoryInterface $subject
     * @param OrderItemSearchResultInterface $searchResult
     *
     * @return OrderItemSearchResultInterface
     */
    public function afterGetList(OrderItemRepositoryInterface $subject, OrderItemSearchResultInterface $searchResult)
    {
        $orderItems = $searchResult->getItems();

        foreach ($orderItems as &$item) {
            $guid = $item->getData(self::FIELD_NAME);
            $extensionAttributes = $item->getExtensionAttributes();
            $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();
            $extensionAttributes->setGuid($guid);
            $item->setExtensionAttributes($extensionAttributes);
        }

        return $searchResult;
    }
}
1
  • 2
    Can you please explain what is your requirement? as you tried to get the guid data using getData() from order item and again setGuid() to order item. If order item has guid value then why need to set again using extension attributes? Aug 9, 2021 at 5:22

1 Answer 1

3
  1. The issue is in extension_attributes.xml you have used Magento\Sales\Api\Data\OrderInterface whereas you tried to set extension attributes for Magento\Sales\Api\Data\OrderItemInterface.
  2. Change the class name from OrderInterface to OrderItemInterface in extension_attributes.xml will solve the issue.
  3. Extension attributes are used to add the additional attributes for the interface, as the interface should not be extended and overwrite in Magento2 to maintain service contracts. For more information, Please visit official document
1
  • this work ok. Thanks
    – HuanTH
    Aug 9, 2021 at 8:39

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.