0

I have created some custom attributes for products and using these attributes in order view in customer account orders section. I have deleted few products. When I view the customer orders, it gives exception because the custom attributes data does not save in sales_order_items table. For this I decided to create extension attributes to save the custom attributes data in sales_order_items entity. I have done the below steps so far but could not able to fetch the custom attributes data in orders view section in customer account.

Step-1 Vendor\Module\registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);

Step-2 Vendor/Module/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Vendor_Module" setup_version="0.0.1">
    </module>
</config>

Step-3 Vendor/Module/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\OrderManagementInterface">
        <plugin name="extension_attribute_plugin" type="Vendor\Module\Plugin\AddExtensionAttributePlugin"/>
    </type>
</config>

Step-4 Vendor/Module/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\OrderItemInterface">
        <attribute code="order_items_extension_attributes" type="string" />
    </extension_attributes>
</config>

Step-5 Vendor/Module/Plugin/AddExtensionAttributePlugin.php

<?php

namespace Vendor\Module\Plugin;

use Magento\Catalog\Model\ProductFactory;

class AddExtensionAttributePlugin
{
    /** @var \Magento\Sales\Api\Data\OrderExtensionFactory */
    protected $orderExtensionFactory;

    /** @var \Magento\Sales\Api\Data\OrderItemExtensionFactory */
    protected $orderItemExtensionFactory;

    /**
     * @param \Magento\Sales\Api\Data\OrderExtensionFactory $orderExtensionFactory
     * @param \Magento\Sales\Api\Data\OrderItemExtensionFactory $orderItemExtensionFactory
     */
    public function __construct(
        \Magento\Sales\Api\Data\OrderExtensionFactory $orderExtensionFactory,
        \Magento\Sales\Api\Data\OrderItemExtensionFactory $orderItemExtensionFactory
    ) {
        $this->orderExtensionFactory = $orderExtensionFactory;
        $this->orderItemExtensionFactory = $orderItemExtensionFactory;
    }


    /**
     * @param \Magento\Sales\Api\OrderManagementInterface $subject
     * @param $resultOrder
     * @return mixed
     */
    public function afterPlace( \Magento\Sales\Api\OrderManagementInterface $subject, $resultOrder) {
        foreach ($resultOrder->getItems() as $orderItem) {
            $extensionAttributes = $orderItem->getExtensionAttributes();
            if (!($extensionAttributes && $extensionAttributes->getOrderItemsExtensionAttributes())) {
                /** @var \Magento\Sales\Api\Data\OrderItemExtension $orderItemExtension */
                $orderItemExtension = $extensionAttributes
                    ? $extensionAttributes
                    : $this->orderItemExtensionFactory->create();
                if ($orderItem->getProduct()->getOrderItemsExtensionAttributes()) {
                    $orderItemExtension->setOrderItemsExtensionAttributes($orderItem->getProduct()->getOrderItemsExtensionAttributes());
                }
                $orderItem->setExtensionAttributes($orderItemExtension);
            }
        }
        $resultOrder->save();
        return $resultOrder;
    }
}

Step-6 Getting the extension attributes in orders view section

$items = $order->getItems();
foreach ($items as $item){
    $extension_attribute_value = $item->getExtensionAttributes()->getOrderItemsExtensionAttributes();
}

Please any one can suggest if I am missing something?

2
  • Are you talking about my account >my order section
    – Amit Bera
    Commented Feb 28, 2023 at 15:58
  • yes, after customer login Commented Feb 28, 2023 at 16:02

1 Answer 1

0

Try below.

$items = $order->getItems();
foreach ($items as $item){
    $extension_attribute_value = $item->getProduct()->getOrderItemsExtensionAttributes();
}

But I dont think this is the fucntionality you wanted because if the product changes the value of this attribute it gonna change on the order. I believe you want to add the attribute value of this specific day of the purchase.

You can do that by creating a column on the sales_order_item and saving there the value of the product attribute with the use of plugin.

I think you didnt understand correctly the use of extention attribute. Here is how you create an extention attribute on the sales order item. Magento 2 how add extension attribute to order item

Here is another good resource to understand the consept https://fooman.com/blog/an-introduction-to-extension-attributes.html

6
  • I want to use deleted products custom attributes data in orders view section. Commented Feb 28, 2023 at 16:55
  • You need to persist this data somewhere and reference it from there. Because if you delete the product then all the attributes value will be deleted with it. Usually if its in the order item then add it in the sales_order_item table. Commented Feb 28, 2023 at 17:22
  • can I achieve it without creating column in the db? Commented Mar 1, 2023 at 8:11
  • The correct and easier way is in database, the more dysfunctional and harder is to save it in file or in some other rest api where you can easily create endpoints and create database tables but it still createing somewhere a database table. Commented Mar 1, 2023 at 10:16
  • what is the purpose of using extension attribute to save data to entity? Commented Mar 1, 2023 at 10:46

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.