5

I want to get product custom options in ordered items by rest/V1/orders. If i use searchCriteria, i received all custom options enter image description here

but in another way i can't resieved custom options. Which method i must extend ?

7

1 Answer 1

4

in my extension i've created file etc/extension_attributes.xml with content

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

create plugin

<type name="Magento\Sales\Api\OrderRepositoryInterface">
        <plugin name="custom_attributes_voc_discount" type="Vendor\Extension\Plugin\OrderGet"/>
    </type>

OrderGet.php

<?php

namespace Vendor\Extension\Plugin;

use Magento\Catalog\Model\ProductFactory;

class OrderGet
{
    /** @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->getVocPrice()) {
                    continue;
                }
                $vocPrice = '5';
                /** @var \Magento\Sales\Api\Data\OrderItemExtension $orderItemExtension */
                $orderItemExtension = $extensionAttributes
                    ? $extensionAttributes
                    : $this->orderItemExtensionFactory->create();
                if($vocPrice) {
                    $orderItemExtension->setVocPrice($vocPrice);
                }

                $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;
    }
}
2
  • Thanks, it's working for me :) Commented Jul 6, 2020 at 11:26
  • For me not working: Fatal Error: 'Uncaught Error: Call to undefined method Magento\\Sales\\Api\\Data\\OrderItemExtension::getVocPrice() on this if line: if ($extensionAttributes && $extensionAttributes->getVocPrice()) Commented Jul 27, 2020 at 11:32

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.