0

I am trying to get the Custom Product Attribute (uom). I want to get the Attribute value to display on the rest/V1/orders API call. m using this https://magento.stackexchange.com/a/303780/68018

app/code/{Vendorname}/{ModuleName}/etc

 <?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="uom" type="string" />
    </extension_attributes>
</config>

namespace {VendorName}{ModuleName}\Plugin\ProductOption.php

<?php


namespace {VendorName}\{ModuleName}\Plugin;

use Magento\Sales\Api\Data\OrderItemInterface;

    class ProductOption
    {
    
    
        /**
         * @var \Magento\Sales\Api\Data\OrderItemExtensionFactory
         */
        private $orderItemExtensionFactory;
    
        public function __construct(
            \Magento\Sales\Api\Data\OrderItemExtensionFactory $orderItemExtensionFactory
        ) {
    
            $this->orderItemExtensionFactory = $orderItemExtensionFactory;
        }
        public function beforeAdd(
            \Magento\Sales\Model\Order\ProductOption $subject,
            OrderItemInterface $orderItem
        ) {
            $extensionAttributes = $orderItem->getExtensionAttributes();
            if(null=== $extensionAttributes){
                $extensionAttributes= $this->orderItemExtensionFactory->create();
    
            }
            $extensionAttributes->setCustomField($order->getCustomField());
            $orderItem->setExtensionAttributes($extensionAttributes);
        }
    }

app/code/{Vendorname}/{ModuleName}/etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="/lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <type name="Magento\Sales\Api\Data\OrderItemInterface">
            <plugin name="custom_attribute_uom" type="Devcrew\CustomAttribute\Plugin\ProductOption"/>
        </type>
    </config>

2 Answers 2

1

You should use the afterGet and/or afterGetList function. Tell me if you need any more help

<?php


namespace {VendorName}\{ModuleName}\Plugin;

use Magento\Sales\Api\Data\OrderItemInterface;

    class ProductOption
    {
    
    
        /**
         * @var \Magento\Sales\Api\Data\OrderItemExtensionFactory
         */
        private $orderItemExtensionFactory;
    
        public function __construct(
            \Magento\Sales\Api\Data\OrderItemExtensionFactory $orderItemExtensionFactory
        ) {
    
            $this->orderItemExtensionFactory = $orderItemExtensionFactory;
        }


        public function afterGet(
            \Magento\Sales\Model\Order\ProductOption $subject,
            OrderItemInterface $orderItem
        ) {
            $extensionAttributes = $orderItem->getExtensionAttributes();
            if(null=== $extensionAttributes){
                $extensionAttributes= $this->orderItemExtensionFactory->create();
    
            }
            $extensionAttributes->setCustomField($order->getCustomField());
            $orderItem->setExtensionAttributes($extensionAttributes);
        }
    }
4
  • Thank u for answer and help :) afterGetList working but it does not show custom attributes. "uom" and its value Commented Jul 2, 2020 at 13:11
  • 1
    Did you add the property to the product or to the order? in your code you are getting the extension attributtes from your orderItem and not from the Product. you should get the product from the order item and then get the attributte from your product, something like this $orderItem->getProduct()->getData('custom_field_name') Tell me if it works ;)
    – Joao71
    Commented Jul 2, 2020 at 13:17
  • really appreciate for your help :) Commented Jul 3, 2020 at 5:42
  • If this solved your question please up vote and mark as answer ! Thanks :)
    – Joao71
    Commented Jul 4, 2020 at 15:56
0

namespace {VendorName}{ModuleName}\Plugin\ProductOption.php

<?php
namespace {VendorName}{ModuleName}\Plugin;

use Magento\Sales\Api\OrderRepositoryInterface;

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

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

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

        public function afterGet(
            \Magento\Sales\Api\OrderRepositoryInterface $subject,
            \Magento\Sales\Api\Data\OrderInterface $resultOrder
        ) {
            $resultOrder = $this->getExtensionAttributesItem($resultOrder);
            return $resultOrder;
        }


          protected function getExtensionAttributesItem(\Magento\Sales\Api\Data\OrderInterface $order)
          {
              $orderItems = $order->getItems();
              if (null !== $orderItems) {
                  /** @var \Magento\Sales\Api\Data\OrderItemInterface $orderItem */
                  foreach ($orderItems as $orderItem) {
                      $extensionAttributes = $orderItem->getExtensionAttributes();
                      if ($extensionAttributes && $extensionAttributes->getUom()) {
                          continue;
                      }
                      $uom = $orderItem->getProduct()->getData('uom');
                      /** @var \Magento\Sales\Api\Data\OrderItemExtension $orderItemExtension */
                      $orderItemExtension = $extensionAttributes
                          ? $extensionAttributes
                          : $this->orderItemExtensionFactory->create();
                      if($uom) {
                          $orderItemExtension->setUom($uom);
                      }

                      $orderItem->setExtensionAttributes($orderItemExtension);
                }
              }
              return $order;
          }



            public function afterGetList(
                \Magento\Sales\Api\OrderRepositoryInterface $subject,
                \Magento\Sales\Model\ResourceModel\Order\Collection $resultOrder
            ) {
                foreach ($resultOrder->getItems() as $order) {
                    $this->afterGet($subject, $order);
                }

                return $resultOrder;
            }
}

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.