0

I ve installed Infobeans OSComments. Module adds additional field on checkout page and save value to Order Page. Is it possible to have access to this comment via REST API ? Can this value cand be add to all data avaible when I use: {{magento_url}}/rest/V1/orders/:id ?

<?php

/**

*/

namespace Infobeans\OSComments\Plugin\Checkout\Model;

class ShippingInformationManagement { // @codingStandardsIgnoreLine protected $quoteRepository;

public function __construct(
    \Magento\Quote\Model\QuoteRepository $quoteRepository
) {
    $this->quoteRepository = $quoteRepository;
}

/**
 * @param \Magento\Checkout\Model\ShippingInformationManagement $subject
 * @param $cartId
 * @param \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
 */
// @codingStandardsIgnoreLine
public function beforeSaveAddressInformation(
    \Magento\Checkout\Model\ShippingInformationManagement $subject,
    $cartId,
    \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
) {
    $extAttributes = $addressInformation->getExtensionAttributes();
    $deliveryComment = htmlspecialchars($extAttributes->getDeliveryComment());
    $quote = $this->quoteRepository->getActive($cartId);
    $quote->setDeliveryComment($deliveryComment);
}

}

Is there easy way to do that ?

2 Answers 2

0

There is no easy way to do this. Here is a way to do this. In my example i did it with order repository but it should be similar with the quote repository just change order by quote everywhere in the code.

Create the file NameSpace/ModuleName/etc/webapi_rest/di.xml . If you use soap you can create the same file here NameSpace/ModuleName/etc/webapi_soap/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\OrderRepositoryInterface">
        <plugin name="orderInformationUpdate" type="NameSpace\ModuleName\Plugin\Api\OrderRepository" />
    </type>
</config>

Then create your plugin NameSpace/ModuleName/Plugin/Api/OrderRepository.php .In my case i had to add 3 attributes to the orders fetched by the api and also add them when retrieving a list of order too.

<?php

namespace NameSpace\ModuleName\Plugin\Api;

use Magento\Sales\Api\Data\OrderExtensionFactory;
use Magento\Sales\Api\Data\OrderItemExtensionFactory;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderSearchResultInterface;
use Magento\Sales\Api\OrderRepositoryInterface;


class OrderRepository
{


    /**
     * Order Extension Attributes Factory
     *
     * @var OrderExtensionFactory
     */
    protected $extensionFactory;

    /**
     * OrderRepository constructor.
     * @param OrderExtensionFactory $extensionFactory
     * @param OrderItemExtensionFactory $itemExtensionFactory
     */
    public function __construct(
        OrderExtensionFactory $extensionFactory,
        OrderItemExtensionFactory $itemExtensionFactory
    )
    {
        $this->extensionFactory = $extensionFactory;
        $this->itemExtensionFactory = $itemExtensionFactory;
    }

    /**
     * Add "delivery_type" extension attribute to order data object to make it accessible in API data
     *
     * @param OrderRepositoryInterface $subject
     * @param OrderInterface $order
     *
     * @return OrderInterface
     */
    public function afterGet(OrderRepositoryInterface $subject, OrderInterface $order)
    {
        $taxAreaCode = $order->getData('tax_area_code')??"";
        $navVatRegistrationNo = $order->getData('nav_vat_registration_no')??"";
        $navTaxExemptionNo = $order->getData('nav_tax_exemption_no')??"";

        $extensionAttributes = $order->getExtensionAttributes();
        $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();
        $extensionAttributes->setTaxAreaCode($taxAreaCode);
        $extensionAttributes->setNavVatRegistrationNo($navVatRegistrationNo);
        $extensionAttributes->setNavTaxExemptionNo($navTaxExemptionNo);
        $order->setExtensionAttributes($extensionAttributes);

        return $order;
    }

    /**
     * Add "delivery_type" extension attribute to order data object to make it accessible in API data
     *
     * @param OrderRepositoryInterface $subject
     * @param OrderSearchResultInterface $searchResult
     *
     * @return OrderSearchResultInterface
     */
    public function afterGetList(OrderRepositoryInterface $subject, OrderSearchResultInterface $searchResult)
    {
        $orders = $searchResult->getItems();

        foreach ($orders as &$order) {
            $taxAreaCode = $order->getData('tax_area_code')??"";
            $navVatRegistrationNo = $order->getData('nav_vat_registration_no')??"";
            $navTaxExemptionNo = $order->getData('nav_tax_exemption_no')??"";

            $extensionAttributes = $order->getExtensionAttributes();
            $extensionAttributes = $extensionAttributes ? $extensionAttributes : $this->extensionFactory->create();
            $extensionAttributes->setTaxAreaCode($taxAreaCode);
            $extensionAttributes->setNavVatRegistrationNo($navVatRegistrationNo);
            $extensionAttributes->setNavTaxExemptionNo($navTaxExemptionNo);
            $order->setExtensionAttributes($extensionAttributes);

        }

        return $searchResult;
    }
}
0

I added to event.xml code

<event name="sales_order_load_after">
        <observer name="sales_order_load_tip_and_trick_attribute" instance="Infobeans\OSComments\Observer\Sales\OrderLoadAfter" />
</event>

OrderLoadAfter.php

<?php

namespace Infobeans\OSComments\Observer\Sales;



use Magento\Framework\Event\ObserverInterface;



class OrderLoadAfter implements ObserverInterface

{

public function execute(\Magento\Framework\Event\Observer $observer)

{

$order = $observer->getOrder();

$extensionAttributes = $order->getExtensionAttributes();



if ($extensionAttributes === null) {

$extensionAttributes = $this->getOrderExtensionDependency();

}

$attr = $order->getData('delivery_comment');

$extensionAttributes->setDeliveryComment($attr);

$order->setExtensionAttributes($extensionAttributes);

}



private function getOrderExtensionDependency()

{

$orderExtension = \Magento\Framework\App\ObjectManager::getInstance()->get(

'\Magento\Sales\Api\Data\OrderExtension'

);

return $orderExtension;

}

}

And works fine. Thank you for reply.

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.