Skip to content

Custom Attribute not saving into the Sales_Order Table

This is my PLugin :

<?php


namespace VendorModulePlugin;

use MagentoSalesApiDataOrderItemExtensionFactory;
use MagentoSalesApiDataOrderItemInterface;
use MagentoSalesApiDataOrderItemSearchResultInterface;
use MagentoSalesApiOrderItemRepositoryInterface;
use MagentoCatalogModelProductFactory;

/**
 * Class OrderItemRepositoryPlugin
 */
class OrderItemRepositoryPlugin
{
    protected $orderItemExtensionFactory;
    protected $productFactory;

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

    /**
     *
     * @param OrderItemRepositoryInterface $subject
     * @param OrderItemInterface $orderItem
     *
     * @return OrderItemInterface
     */
    public function afterGet(OrderItemRepositoryInterface $subject, OrderItemInterface $orderItem)
    {
        $customAttribute = $orderItem->getData('custom_attribute');

        $extensionAttributes = $orderItem->getExtensionAttributes();
        $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();

        $extensionAttributes->setCustomAttribute($customAttribute);
        $orderItem->setExtensionAttributes($extensionAttributes);

        return $orderItem;
    }

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

        foreach ($orders as &$order) {
            $order = $this->getOrderItemExtensionAttributes($order);
        }

        return $searchResult;
    }

    /**
     *
     * @param OrderItemInterface $orderItem
     *
     * @return OrderItemInterface
     */
    protected function getOrderItemExtensionAttributes(OrderItemInterface $orderItem)
    {
        $product = $this->productFactory->create();
        $product->load($orderItem->getProductId());
        $customAttribute = $product->getCustomAttribute();

        if (isset($customAttribute)) {
            $orderItemExtension = $this->orderItemExtensionFactory->create();
            $orderItemExtension->setCustomAttribute($customAttribute);

            $orderItem->setExtensionAttributes($orderItemExtension);
        }

        return $orderItem;
    }
}

I am getting my custom attribute data in this but not saving in the sales_order tabe. I created extension_attributes.xml for the attribute.

However I saved the same attribute value by using the observer in the sales_order_item table.

public function execute(Observer $observer)
    {
        /** @var MagentoSalesModelOrder $order */
        $order = $observer->getEvent()->getOrder();
        /** @var MagentoQuoteModelQuote $quote */
        $quote = $observer->getEvent()->getQuote();
        foreach ($order->getItems() as $orderItem) {
            $quoteItem = $quote->getItemById($orderItem->getQuoteItemId());
            if ($quoteItem) {
                $customAttribute = $quoteItem->getCustomAttribute();
                if ($customAttribute) {
                    $orderItem->setCustomAttribute($customAttribute);
                }
            }
        }
    }

The file of the extension_attributes.xml file is :

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Api/etc/extension_attributes.xsd">
    <!-- Extension attribute for OrderInterface -->
    <extension_attributes for="MagentoSalesApiDataOrderInterface">
        <attribute code="custom_attribute" type="float" />
    </extension_attributes>
    <!-- Extension attribute for OrderItemInterface -->
    <extension_attributes for="MagentoSalesApiDataOrderItemInterface">
        <attribute code="custom_attribute" type="float" />
    </extension_attributes>

</config>