4

I have created extension attributes to order for rest API services. Also, I'm able to display the attributes on the order grid.

How can I display the same attributes on admin create order form and save it?

Here is my extension_attributes.xml file

<?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\Checkout\Api\Data\ShippingInformationInterface">
        <attribute code="delivery_day_slot" type="string"/>
    </extension_attributes>

    <extension_attributes for="Magento\Sales\Api\Data\OrderInterface">
        <attribute code="delivery_day_slot" type="string"/>
    </extension_attributes>
</config>

And, I'm saving the data using plugin

<type name="Magento\Checkout\Model\ShippingInformationManagement">
    <plugin name="NM_save_delivery_slot_in_quote" type="<Namespace>\<ModuleName>\Plugin\Checkout\Model\ShippingInformationManagement" sortOrder="1"/>
</type>

Namespace\ModuleName\Plugin\Checkout\Model\ShippingInformationManagement

namespace <Namespace>\<ModuleName>\Plugin\Checkout\Model;

class ShippingInformationManagement
{
    protected $quoteRepository;

    public function __construct(
        \Magento\Quote\Model\QuoteRepository $quoteRepository,
        \Magento\Checkout\Api\Data\ShippingInformationExtensionFactory $shpExtensionFactory,
        \Psr\Log\LoggerInterface $logger
    ) {
        $this->quoteRepository = $quoteRepository;
        $this->shpExtensionFactory = $shpExtensionFactory;
        $this->logger = $logger;
    }

    /**
     * @param \Magento\Checkout\Model\ShippingInformationManagement $subject
     * @param $cartId
     * @param \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
     */
    public function beforeSaveAddressInformation(
        \Magento\Checkout\Model\ShippingInformationManagement $subject,
        $cartId,
        \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
    ) {

        $extensionAttributes = $addressInformation->getExtensionAttributes();
        $shipExtension = $extensionAttributes ? $extensionAttributes : $this->shpExtensionFactory->create();

        $payload = \file_get_contents("php://input");
        $attr = json_decode($payload, true);
        if(isset($attr["extension_attributes"])){
            $deliveryDaySlot = $attr["extension_attributes"]["delivery_day_slot"];

            $addressInformation->setDeliveryDaySlot($deliveryDaySlot);
            $quote = $this->quoteRepository->getActive($cartId);
            $quote->setDeliveryDaySlot($deliveryDaySlot);

        }

        return;
    }
}

Can any one please suggest a best approach?

Thanks in advance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.