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 VendorModuleregistration.php
<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::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="MagentoSalesApiOrderManagementInterface">
<plugin name="extension_attribute_plugin" type="VendorModulePluginAddExtensionAttributePlugin"/>
</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="MagentoSalesApiDataOrderItemInterface">
<attribute code="order_items_extension_attributes" type="string" />
</extension_attributes>
</config>
Step-5 Vendor/Module/Plugin/AddExtensionAttributePlugin.php
<?php
namespace VendorModulePlugin;
use MagentoCatalogModelProductFactory;
class AddExtensionAttributePlugin
{
/** @var MagentoSalesApiDataOrderExtensionFactory */
protected $orderExtensionFactory;
/** @var MagentoSalesApiDataOrderItemExtensionFactory */
protected $orderItemExtensionFactory;
/**
* @param MagentoSalesApiDataOrderExtensionFactory $orderExtensionFactory
* @param MagentoSalesApiDataOrderItemExtensionFactory $orderItemExtensionFactory
*/
public function __construct(
MagentoSalesApiDataOrderExtensionFactory $orderExtensionFactory,
MagentoSalesApiDataOrderItemExtensionFactory $orderItemExtensionFactory
) {
$this->orderExtensionFactory = $orderExtensionFactory;
$this->orderItemExtensionFactory = $orderItemExtensionFactory;
}
/**
* @param MagentoSalesApiOrderManagementInterface $subject
* @param $resultOrder
* @return mixed
*/
public function afterPlace( MagentoSalesApiOrderManagementInterface $subject, $resultOrder) {
foreach ($resultOrder->getItems() as $orderItem) {
$extensionAttributes = $orderItem->getExtensionAttributes();
if (!($extensionAttributes && $extensionAttributes->getOrderItemsExtensionAttributes())) {
/** @var MagentoSalesApiDataOrderItemExtension $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?