-1

How to set the data in the extension attribute which i created and how to create more the one extension attribute in single module. My Following code is in below link.

URL : My Following code structure

Thanks in Advance :)

1 Answer 1

1

Let's assume that you have three fields then you to add like at

Sm\OrderFeedback\etc\extension_attributes.xml

<?xml version="1.0"?>
<!-- File: app/code/Sm/OrderFeedback/etc/extension_attributes.xml -->
<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="customer_feedback" type="string" />
        <attribute code="customfieldone" type="string" /> <!--assume that field type string-->
        <attribute code="customfieldtwo" type="int" /> <!--assume that field type integer-->
    </extension_attributes>
</config>

Add customfieldone,customfieldtwo to Sm\OrderFeedback\Plugin\OrderRepositoryPlugin.php for save using before plugin on Magento\Sales\Api\OrderRepositoryInterface:save.

<?php
/* File: app/code/Sm/OrderFeedback/Plugin/OrderRepositoryPlugin.php */

namespace Sm\OrderFeedback\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;

/**
 * Class OrderRepositoryPlugin
 */
class OrderRepositoryPlugin
{
    /**
     * Order feedback field name
     */
    const FIELD_NAME = 'customer_feedback';

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

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

    /**
     * Add "customer_feedback" 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)
    {
        $customerFeedback = $order->getData(self::FIELD_NAME);
        $extensionAttributes = $order->getExtensionAttributes();
        $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();
        $extensionAttributes->setCustomerFeedback($customerFeedback);
        $order->setExtensionAttributes($extensionAttributes);

        return $order;
    }

    /**
     * Add "customer_feedback" 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) {
            $customerFeedback = $order->getData(self::FIELD_NAME);
            $extensionAttributes = $order->getExtensionAttributes();
            $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();
            $extensionAttributes->setCustomerFeedback($customerFeedback);
            $order->setExtensionAttributes($extensionAttributes);
        }

        return $searchResult;
    }

    /** save fields**/
    public function beforeSave(
      OrderRepositoryInterface $subject,  
      OrderInterface $order
    ){
         $extensionAttributes = $order->getExtensionAttributes();

         if($extensionAttributes !== null){
            $customfieldOne =$order->getExtensionAttributes()->getCustomfieldone();
            $customfieldtwo =$order->getExtensionAttributes()->getCustomfieldtwo();
            if($customfieldOne !== null) {
               $order->setData('customfieldone',$customfieldOne);
            }
            if($customfieldtwo !== null) {
                $order->setData('customfieldtwo',$customfieldtwo);
            }            
         }
         return $order;
    }
}

If you set custom field value using setter function $order->setData('fieldName',$Fieldvalue) on beforeSave() plugin method to order object and Resource mode automatically save to sale_order table like $order->setData('customfieldtwo',$customfieldtwo);

To better understand how extension attribute will implement on Magento 2 ,checkout the blog https://store.fooman.co.nz/blog/an-introduction-to-extension-attributes.html

2
  • But when using Rest api i can't insert the value in the extension attribute.@amit Bera
    – Sabareesh
    Commented Apr 15, 2020 at 5:09
  • First, you have to pass the custom field using extension attributes "extension_attributes": {'customfield':'value1'} and also have to add filed on sales_order tables.
    – Amit Bera
    Commented Apr 15, 2020 at 5:23

Your Answer

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

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