I can't set the value for my extension attribute , how to set the value for extension attribute using json array swagger. And i can't show the data in the order grid.

The below attribute and value can't set.

My attribute : customer_feedback

Value : Hello

My Module that created for extension attribute

Sm\OrderFeedback\etc\di.xml

<?xml version="1.0"?>
<!-- File: app/code/Sm/OrderFeedback/etc/di.xml -->
<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="sm_orderfeedback_add_order_extension_attribute"
                type="Sm\OrderFeedback\Plugin\OrderRepositoryPlugin" />
    </type>
</config>

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" />
    </extension_attributes>
</config>

Sm\OrderFeedback\Setup\InstallSchema.php

namespace Sm\OrderFeedback\Setup;

use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

/**
 * Class InstallSchema
 */
class InstallSchema implements InstallSchemaInterface
{
    /**
     * Custom order column
     */
    const ORDER_FEEDBACK_FIELD = 'customer_feedback';

    /**
     * @inheritdoc
     */
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();
        $setup->getConnection()->addColumn(
            $setup->getTable('sales_order'),
            self::ORDER_FEEDBACK_FIELD,
            [
                'type' => Table::TYPE_TEXT,
                'size' => 255,
                'nullable' => true,
                'comment' => 'Customer Feedback'
            ]
        );
        $setup->endSetup();
    }
}

Sm\OrderFeedback\Plugin\OrderRepositoryPlugin.php

<?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;
    }
}

My Request API

 {
  "entity": {
        "base_currency_code": "USD",
        "base_discount_amount": -4.5,
        "base_grand_total": 45.5,
        "base_shipping_amount": 5,
        "base_subtotal": 45,
        "base_tax_amount": 0,
        "customer_email": "Rbjah@test.com",
        "customer_firstname": "vins",
        "customer_group_id": 1,
        "customer_id": 200,
        "customer_is_guest": 0,
        "customer_lastname": "RBJ",
        "customer_note_notify": 1,
        "discount_amount": -4.5,
        "email_sent": 1,
        "coupon_code": "Test1",
        "discount_description": "Test1",
        "grand_total": 45.5,
        "is_virtual": 0,
        "order_currency_code": "USD",
        "shipping_amount": 5,
        "shipping_description": "Flat Rate - Fixed",
        "state": "new",
        "status": "pending",
        "store_currency_code": "USD",
        "store_id": 1,
        "store_name": "Main Website\nMain Website Store\n",
        "subtotal": 45,
        "subtotal_incl_tax": 45,
        "tax_amount": 0,
        "total_item_count": 19,
        "total_qty_ordered": 19,
        "weight": 1,
        "items": [
        {
            "base_discount_amount": 4.5,
            "base_original_price": 45,
            "base_price": 45,
            "base_price_incl_tax": 45,
            "base_row_invoiced": 0,
            "base_row_total": 45,
            "base_tax_amount": 0,
            "base_tax_invoiced": 0,
            "discount_amount": 4.5,
            "discount_percent": 10,
            "free_shipping": 0,
            "is_virtual": 0,
            "name": "Push It Messenger Bag",
            "original_price": 45,
            "price": 20,
            "price_incl_tax": 45,
            "product_id": 14,
            "product_type": "simple",
            "qty_ordered": 12,
            "row_total": 240,
            "row_total_incl_tax": 240,
            "sku": "24-WB04",
            "store_id": 1
        },{
            "base_discount_amount": 4.5,
            "base_original_price": 45,
            "base_price": 27.00,
            "base_price_incl_tax": 45,
            "base_row_invoiced": 0,
            "base_row_total": 45,
            "base_tax_amount": 0,
            "base_tax_invoiced": 0,
            "discount_amount": 4.5,
            "discount_percent": 10,
            "free_shipping": 0,
            "is_virtual": 0,
            "name": "Sprite Stasis Ball 65 cm",
            "original_price": 45,
            "price": 10,
            "price_incl_tax": 45,
            "product_id": 14,
            "product_type": "simple",
            "qty_ordered": 1,
            "row_total": 10,
            "row_total_incl_tax": 10,
            "sku": "24-WG082-pink",
            "store_id": 1
        }
        ],
        "billing_address": {
            "address_type": "billing",
            "city": "Ahmedabad",
            "company": "Rbj",
            "country_id": "US",
            "email": "Rbjah@test.com",
            "firstname": "Rbjah",
            "lastname": "sasasa",
            "postcode": "30332",
            "region": "Georgia",
            "region_code": "GA",
            "region_id": 19,
            "street": [
                "Street 1",
                "Street 2"
            ],
            "telephone": "123456"
        },
        "payment": {
            "method": "cashondelivery"
        },
        "extension_attributes": {
            "shipping_assignments": [
                {
                    "shipping": {
                        "address": {
                            "address_type": "shipping",
                            "city": "Ahmedabad",
                            "company": "Rbjah",
                            "country_id": "US",
                            "customer_address_id": 2,
                            "email": "Rbjah@test.com",
                            "firstname": "Rbjah",
                            "lastname": "sasasa",
                            "postcode": "30332",
                            "region": "Georgia",
                            "region_code": "GA",
                            "region_id": 19,
                            "street": [
                                "Street 1",
                                "Street 2"
                            ],
                            "telephone": "123456"
                        },
                        "method": "flatrate_flatrate"
                    }
                }
            ],
      "customer_feedback": "Hello"
        }
    }
}

Thanks in advance :)

有帮助吗?

解决方案

Once you save your attribute customer_feedback value in table sales_order. Then add this two files in your module.

app/code/VendoreName/MoudleName/view/adminhtml/layout

sales_order_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="styles"/>
    <body>
        <referenceContainer name="content">
            <uiComponent name="sales_order_grid"/>
        </referenceContainer>
    </body>
</page>

app/code/VendoreName/MoudleName/view/adminhtml/ui_component

sales_order_grid.xml

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <columns name="sales_order_columns">
        <column name="customer_feedback">
            <settings>
                <filter>text</filter>
                <label translate="true">Customer Feedback</label>
                <visible>false</visible>
            </settings>
        </column>
    </columns>
</listing>

Run Magento Command

php bin/magento s:up
php bin/magento s:s:d -f
php bin/magento c:f

if not see your column then click on Columns like below screenshot, tick you column check box.

enter image description here

I Hope This Helps You.

许可以下: CC-BY-SA归因
scroll top