0

How to show a advance inventory custom field value in sales order view page in frontend and backend in magento 2

Product edit page in admin

enter image description here

Frontend:

t e

Backend:

i

please someone can me help for this

1

To display product attributes in order history items and admin panel you need to create extra column in sales_order_item table.

STEP1: In your Vendor\Module\Setup\UpgradeSchema.php

<?php

namespace Vendor\Module\Setup;

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

class UpgradeSchema implements UpgradeSchemaInterface
{
    /**
     * Upgrades DB schema for a module
     *
     * @param SchemaSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        $orderTable = 'sales_order_item';

        //Order table
        $setup->getConnection()
            ->addColumn(
                $setup->getTable($orderTable),
                'preorder_note',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'length' => 255,
                    'comment' =>'Product pre order'
                ]
            );

        $setup->endSetup();
    }
}

STEP2: Create plugin to save the pre order value in sales_order_items table. In you di.xml create a plugin.

<?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\Quote\Model\Quote\Item\ToOrderItem">
        <plugin name="unique_name" type="Vendor\Module\Plugin\ToOrderItem" sortOrder="1" />
    </type>
</config>

Vendor\Module\Plugin\ToOrderItem.php

 <?php
namespace Vendor\Module\Plugin;

use Magento\Quote\Model\Quote\Item\ToOrderItem as QuoteToOrderItem;
use Magento\Catalog\Api\ProductRepositoryInterface;


class ToOrderItem
{
    /*
    * @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
    */
    protected $productRepository;
    protected $stockItem;


    public function __construct(
        ProductRepositoryInterface $productRepository,
         \Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItem
    ) {
        $this->productRepository = $productRepository;
        $this->stockItem = $stockItem;
    }

    /**
     * aroundConvert
     *
     * @param QuoteToOrderItem $subject
     * @param \Closure $proceed
     * @param \Magento\Quote\Model\Quote\Item $item
     * @param array $data
     *
     * @return \Magento\Sales\Model\Order\Item
     */
    public function aroundConvert(
        QuoteToOrderItem $subject,
        \Closure $proceed,
        $item,
        $data = []
    ) {
        $orderItem = $proceed($item, $data);
        $product = $this->productRepository->getById($orderItem->getProduct()->getId());

        if(!empty($product->getId())) {

        $productStock = $this->stockItem->get($product->getId());
        $preorder= $productStock->getData('preorder'); 

            // $product->getPreOrder() is product attribute
            $orderItem->setPreOrder($preorder);
        }
        return $orderItem;
    }
}

STEP3: To display pre order notes in front end order history, override app/design/frontend/Vendor/Theme/Magento_Sales/templates/order/items/renderer/default.phtml you can call pre order value of ordered items by

$_item->getPreorder();

STEP4: To display this in admin panel override sales_order_view.xml in vendor/magento/module-sales/view/adminhtml/layout/sales_order_view.xml and you can see this block override this block template with your custom template. Hope you know how to override a xml file template.

<block class="Magento\Sales\Block\Adminhtml\Items\Column\Name" name="column_name" template="Magento_Sales::items/column/name.phtml" group="column"/>

In your custom template copy all the code in vendor/magento/module-sales/view/adminhtml/templates/items/column/name.phtml to your custom template and add $_item->getPreorder(); this line of code to display the pre order notes in admin panel where you want.

STEP5: To display in sales order grid create Vendor/Module/view/adminhtml/ui_component/sales_order_grid.xml

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <listingToolbar name="listing_top"/>
    <columns name="sales_order_columns">
        <column name="preorder_notes" class="Vendor\Module\Ui\Component\Listing\Column\Sales\OrderItems">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Pre order</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

Vendor\Module\Ui\Component\Listing\Column\Sales\OrderItems.php

<?php
namespace Vendor\Module\Ui\Component\Listing\Column\Sales;


use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;

class OrderItems extends \Magento\Ui\Component\Listing\Columns\Column
{
    protected $orderInterface;
    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        \Magento\Sales\Api\Data\OrderInterface $orderInterface,
        array $components = [],
        array $data = []
    ){
        parent::__construct($context, $uiComponentFactory, $components, $data);
        $this->_orderInterface = $orderInterface;
    }

    public function prepareDataSource(array $dataSource) {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as & $item) {
                $orderId = $item['increment_id'];
                $orderObj = $this->_orderInterface->loadByIncrementId($orderId);
                $result = '';
                foreach ($orderObj->getAllItems() as $orderItem) {
                    $result .= $orderItem->getPreorder().', ';
                }
                $item['preorder_notes'] = $result;
            }
        }
        return $dataSource;
    }
}

It will display comma separated when the order has multiple order items

  • value does save in sales_order_item – divya sekar May 20 at 6:34
  • What is your pre-order attribute code? – Ranganathan May 20 at 6:37
  • <attribute code="preorder_note" type="string"/> – divya sekar May 20 at 6:39
  • its working, typo mistake done by me – divya sekar May 20 at 6:41
  • Please check ToOrderItem.php edits match with your file. – Ranganathan May 20 at 6:41

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.