cancel
Showing results for 
Search instead for 
Did you mean: 

Set order custom attributes via order Rest API in Magento 2.3.3

Set order custom attributes via order Rest API in Magento 2.3.3

I have problem with save value of order custom attribute via Rest API in Magento 2.3.3. I get only "Internal Error", with log:

 

Property "GremiOrderId" does not have accessor method "getGremiOrderId" in class "Magento\Sales\Api\Data\OrderInterface"

 

 

Below you can see our code from custom module:

 

Gremi/OrderAttributes/etc/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\OrderRepositoryInterface">
        <plugin name="gremi_orderattributes_add_order_extension_attribute"
                type="Gremi\OrderAttributes\Plugin\OrderRepositoryPlugin" />
    </type>
</config>

 

Gremi/OrderAttributes/etc/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="gremi_order_id" type="Gremi\OrderAttributes\Api\Data\GremiOrderIdInterface" />
        </extension_attributes>
</config>

 

 

Gremi/OrderAttributes/Api/Data/GremiOrderIdInterface.php:

 

<?php
namespace Gremi\OrderAttributes\Api\Data;

/**
 * Gremi OrderAttributes block interface.
 * @api
 * @since 100.0.2
 */
interface GremiOrderIdInterface extends \Magento\Sales\Api\Data\OrderInterface
{
    /**#@+
     * Constants for keys of data array. Identical to the name of the getter in snake case
     */
    const GREMI_ORDER_ID        = 'gremi_order_id';

    /**
     * Get GREMI_ORDER_ID
     *
     * @return string|null
     */
    public function getGremiOrderId();

    /**
     * Set GREMI_ORDER_ID
     *
     * @param string $gremiOrderId
     * @return $this
     */
    public function setGremiOrderId($gremiOrderId);

}

 

Gremi/OrderAttributes/Plugin/OrderRepositoryPlugin.php:

<?php
namespace Gremi\OrderAttributes\Plugin;

use Magento\Sales\Api\Data\OrderExtensionFactory;
use Magento\Sales\Api\Data\OrderExtensionInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderSearchResultInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Framework\Exception\CouldNotSaveException;


/**
 * Class OrderRepository
 */
class OrderRepositoryPlugin
{

    const FIELD_GREMI_ORDER_ID = 'gremi_order_id';

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

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

    /**
     * Add "gremi_order_id" extension attribute to order data object to make it accessible in API data
     *
     * @param OrderRepositoryInterface $subject
     * @param OrderInterface $order
     *
     * @return OrderInterface
     */
    public function afterGet(OrderRepositoryInterface $subject, OrderInterface $order)
    {
        $gremiOrderId = $order->getData(self::FIELD_GREMI_ORDER_ID);
        $extensionAttributes = $order->getExtensionAttributes();
        $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();
        $extensionAttributes->setGremiOrderId($gremiOrderId);
        $order->setExtensionAttributes($extensionAttributes);

        return $order;
    }

    /**
     * Add "gremi_order_id" extension attribute to order data object to make it accessible in API data
     *
     * @param OrderRepositoryInterface $subject
     * @param OrderSearchResultInterface $searchResult
     *
     * @return OrderSearchResultInterface
     */
    public function afterGetList(OrderRepositoryInterface $subject, OrderSearchResultInterface $searchResult)
    {
        $orders = $searchResult->getItems();

        foreach ($orders as &$order) {
            $gremiOrderId = $order->getData(self::FIELD_GREMI_ORDER_ID);
            $extensionAttributes = $order->getExtensionAttributes();
            $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();
            $extensionAttributes->setGremiOrderId($gremiOrderId);
            $order->setExtensionAttributes($extensionAttributes);
        }

        return $searchResult;
    }

    public function beforeSave(OrderRepositoryInterface $subject, OrderInterface $resultOrder)
    {
        foreach (['gremi_order_id'] as $field) {
            $value = $resultOrder->getData($field);
            $resultOrder->setData($field, $value);
        }
    }

}

What I should do it, to works correctly?

12 REPLIES 12

Re: Set order custom attributes via order Rest API in Magento 2.3.3

hi @x_xx 

 

could you please try first using type="string" in extension attribute type instead of a class. 

This may help you.

 

Thanks!

Re: Set order custom attributes via order Rest API in Magento 2.3.3

Thank you for reply, but still not working. The same error in log.

Re: Set order custom attributes via order Rest API in Magento 2.3.3

Can you try moving your di file in app/code/<namespeace>/<module>/etc/webapi_rest folder. In my case, i have added di file in above folder.

 

Tr to flush cache and run di:compile after that.

Re: Set order custom attributes via order Rest API in Magento 2.3.3

Yes, it done. Still the same error.

Re: Set order custom attributes via order Rest API in Magento 2.3.3

Hey @x_xx ,

 

Is there any way that i can access your code and check .?

 

Please suggest!

 

Thanks!

Re: Set order custom attributes via order Rest API in Magento 2.3.3

Re: Set order custom attributes via order Rest API in Magento 2.3.3

Hi,
did you see the code? Do you have any ideas to solve my problem?

Re: Set order custom attributes via order Rest API in Magento 2.3.3

Hi @x_xx ,

 

Yes, i checked the code and found that observer file is missing. Is it in the code which you shared with me or in the module itself. Could you please check once.

 

I have done the below changes in the webapi_rest/di.xml and found that our custom attribute is occurring in api call.  Could you please try with this.

 

xsi:noNamespaceSchemaLocation="urn:magento:frameworkSmiley SurprisedbjectManager/etc/config.xsd">
<!-- <preference for="Magento\Sales\Api\Data\OrderInterface" type="Gremi\OrderAttributes\Api\Data\GremiOrderIdInterface"/> -->
<!-- <type name="Magento\Sales\Api\OrderRepositoryInterface">
<plugin name="gremi_orderattributes_add_order_extension_attribute"
type="string" />
</type> -->
<type name="Magento\Sales\Api\OrderRepositoryInterface">
<plugin name="gremi_orderattributes_add_order_extension_attribute" type="Gremi\OrderAttributes\Plugin\OrderRepositoryPlugin" />
</type>
</config>
 
please check once.

Re: Set order custom attributes via order Rest API in Magento 2.3.3

Are you able to solve this i am also getting this same issue