0

I have created a sales attribute "port" in sale_order table. I want to POST this post attribute in shipping information API payload and get it in response as extension attribute. I also want to save the value of extension attribute to save in DB using Api only i.e. there is no extra filed for port in backend or frontend. Please help. Thanks in advance.

Vendor/Module/Setup/InstallSchema.php

<?php

namespace Vendor\Module\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;

class InstallSchema implements InstallSchemaInterface
{
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $tableName = $setup->getTable('sales_order_grid');

        $tableNameSales = $setup->getTable('sales_order');
        $tableNameQuote = $setup->getTable('quote');

        // Check if the table already exists

        $columns = [
            'delivery_date' => [
            'type' => 'datetime',
            'nullable' => true,
            'comment' => 'Delivery Date',
            ],
            'no_of_days' => [
            'type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            'nullable' => true,
            'comment' => 'Number of Days',
            ],
            'no_of_crew' => [
            'type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            'nullable' => true,
            'comment' => 'Number of Crew',
            ],
            'port' => [
            'type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
            'nullable' => true,
            'comment' => 'Port',
            ]
        ];

        $connection = $setup->getConnection();

        foreach ($columns as $name => $definition) {
            $connection->addColumn($tableName, $name, $definition);
            $connection->addColumn($tableNameSales, $name, $definition);
            $connection->addColumn($tableNameQuote, $name, $definition);
        }

        $setup->endSetup();
    }
}

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

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="Magento\Checkout\Model\ShippingInformationManagement">
        <plugin name="sr_save_delivery_date_in_quote" type="Vendor\Module\Plugin\Checkout\Model\ShippingInformationManagement" sortOrder="1"/>
    </type>
    <virtualType name="Magento\Sales\Model\ResourceModel\Order\Grid" type="Magento\Sales\Model\ResourceModel\Grid">
        <arguments>
            <argument name="columns" xsi:type="array">
                <item name="delivery_date" xsi:type="string">sales_order.delivery_date</item>
            </argument>
        </arguments>
    </virtualType>
</config>

Vendor/Module/Plugin/Checkout/Model/ShippingInformationManagement.php

<?php
namespace Vendor\Module\Plugin\Checkout\Model;
class ShippingInformationManagement
{
    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
     */
    public function beforeSaveAddressInformation(
        \Magento\Checkout\Model\ShippingInformationManagement $subject,
        $cartId,
        \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
    ) {
        $extAttributes = $addressInformation->getExtensionAttributes();
        $deliveryDate = $extAttributes->getDeliveryDate();
        $quote = $this->quoteRepository->getActive($cartId);
        $quote->setDeliveryDate($deliveryDate);
    }
}

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.