0

How to show a advance inventory custom field value in checkout order summary in magento 2

Product edit page in admin

enter image description here

checkout page

enter image description here

code:

 <?php
namespace Cm\Preorder\Plugin\Checkout\Model;

use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Catalog\Model\ProductRepository as ProductRepository;
use Magento\CatalogInventory\Model\Stock\StockItemRepository;

class DefaultConfigProvider extends \Magento\Framework\Model\AbstractModel
{
    protected $checkoutSession;

    protected $stockItem;

    protected $_productRepository;

    private $_objectManager;



    public function __construct(
        CheckoutSession $checkoutSession,
        ProductRepository $productRepository,
        \Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItem,
          \Magento\Framework\ObjectManagerInterface $objectmanager
    ) {
        $this->checkoutSession = $checkoutSession;
        $this->_productRepository = $productRepository;
        $this->stockItem = $stockItem;
        $this->_objectManager = $objectmanager;

    }

    public function afterGetConfig(
        \Magento\Checkout\Model\DefaultConfigProvider $subject, 
        array $result
    ) {
        $items = $result['totalsData']['items'];
        foreach ($items as $index => $item) {
            $quoteItem = $this->checkoutSession->getQuote()->getItemById($item['item_id']);
            $product = $this->_productRepository->getById($quoteItem->getProduct()->getId());

            $productId = $product->getId();
            $stock = $this->stockItem->get($productId);
        /*    $preorder_note = $stock->getData('preorder_note'); */
            $result['quoteItemData'][$index]['preorder_note'] =$productId;
        }
        return $result;
    }


}
2

Try the following way:

app/code/SR/MagentoCommunity/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\Quote\Model\Cart\CartTotalRepository">
        <plugin name="checkout_item_sidebar"
                type="SR\MagentoCommunity\Plugin\Quote\Model\Cart\CartTotalRepository" sortOrder="1"/>
    </type>
</config>

app/code/SR/MagentoCommunity/Plugin/Quote/Model/Cart/CartTotalRepository.php

<?php
namespace SR\MagentoCommunity\Plugin\Quote\Model\Cart;

use Magento\CatalogInventory\Model\Stock\StockItemRepository;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\TotalsItemExtensionFactory;

class CartTotalRepository
{
    /**
     * @var StockItemRepository
     */
    private $stockItemRepository;

    /**
     * @var CartRepositoryInterface
     */
    private $cartRepository;

    /**
     * @var TotalsItemExtensionFactory
     */
    private $extensionFactory;

    /**
     * CartTotalRepository constructor.
     *
     * @param StockItemRepository $stockItemRepository
     * @param CartRepositoryInterface $cartRepository
     * @param TotalsItemExtensionFactory $extensionFactory
     */
    public function __construct(
        StockItemRepository $stockItemRepository,
        CartRepositoryInterface $cartRepository,
        TotalsItemExtensionFactory $extensionFactory
    ) {
        $this->stockItemRepository = $stockItemRepository;
        $this->cartRepository = $cartRepository;
        $this->extensionFactory = $extensionFactory;
    }

    public function aroundGet(
        \Magento\Quote\Model\Cart\CartTotalRepository $subject,
        \Closure $proceed,
        $cartId
    ) {
        $quoteTotals = $proceed($cartId);
        /** @var \Magento\Quote\Model\Quote $quote */
        $quote = $this->cartRepository->getActive($cartId);
        $quoteItemsArray = [];
        foreach ($quote->getAllVisibleItems() as $index => $item) {
            $productId = $item->getProductId();
            $stock = $this->getStockItem($productId);
            $quoteItemsArray[$item->getItemId()] = $stock;
        }
        $quoteItems = $quoteTotals->getItems();
        $items = [];

        /**  @var \Magento\Quote\Model\Cart\Totals\Item $item */
        foreach ($quoteItems as $index => $item) {
            $extensionAttributes = $item->getExtensionAttributes();

            if ($extensionAttributes === null) {
                $extensionAttributes = $this->extensionFactory->create();
            }

            $preorderNote = '';
            if (isset($quoteItemsArray[$item->getItemId()])) {
                $preorderNote = $quoteItemsArray[$item->getItemId()]['preorder_note'];
            }

            $extensionAttributes->setPreorderNote($preorderNote);
            $item->setExtensionAttributes($extensionAttributes);
            $items[$index] = $item;
        }

        $quoteTotals->setItems($items);
        return $quoteTotals;
    }

    /**
     * @param $productId
     * @return \Magento\CatalogInventory\Api\Data\StockItemInterface
     */
    public function getStockItem($productId)
    {
        return $this->stockItemRepository->get($productId);
    }
}

app/code/SR/MagentoCommunity/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\Quote\Api\Data\TotalsItemInterface">
        <attribute code="preorder_note" type="string"/>
    </extension_attributes>
</config>

Now overwrite following class and add following code snippet

magento/module-checkout/view/frontend/web/template/summary/item/details.html

<!-- ko if: $parent.extension_attributes -->
    <!-- ko if: $parent.extension_attributes.preorder_note -->
        <div data-bind="text: $parent.extension_attributes.preorder_note"></div>
    <!-- /ko -->
<!-- /ko -->

Now looks like:

<!--
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
-->

<!-- ko foreach: getRegion('before_details') -->
    <!-- ko template: getTemplate() --><!-- /ko -->
<!-- /ko -->
<div class="product-item-details">

    <div class="product-item-inner">
        <div class="product-item-name-block">
            <strong class="product-item-name" data-bind="html: $parent.name"></strong>
            <div class="details-qty">
                <span class="label"><!-- ko i18n: 'Qty' --><!-- /ko --></span>
                <span class="value" data-bind="text: $parent.qty"></span>
            </div>
        </div>
        <!-- ko foreach: getRegion('after_details') -->
            <!-- ko template: getTemplate() --><!-- /ko -->
        <!-- /ko -->
        <!-- ko if: $parent.extension_attributes -->
            <!-- ko if: $parent.extension_attributes.preorder_note -->
                <div data-bind="text: $parent.extension_attributes.preorder_note"></div>
            <!-- /ko -->
        <!-- /ko -->
    </div>

    <!-- ko if: (JSON.parse($parent.options).length > 0)-->
    <div class="product options" data-bind="mageInit: {'collapsible':{'openedState': 'active'}}">
        <span data-role="title" class="toggle"><!-- ko i18n: 'View Details' --><!-- /ko --></span>
        <div data-role="content" class="content">
            <strong class="subtitle"><!-- ko i18n: 'Options Details' --><!-- /ko --></strong>
            <dl class="item-options">
                <!--ko foreach: JSON.parse($parent.options)-->
                <dt class="label" data-bind="text: label"></dt>
                    <!-- ko if: ($data.full_view)-->
                    <dd class="values" data-bind="html: full_view"></dd>
                    <!-- /ko -->
                    <!-- ko ifnot: ($data.full_view)-->
                    <dd class="values" data-bind="text: value"></dd>
                    <!-- /ko -->
                <!-- /ko -->
            </dl>
        </div>
    </div>
    <!-- /ko -->
</div>
<!-- ko foreach: getRegion('item_message') -->
    <!-- ko template: getTemplate() --><!-- /ko -->
<!-- /ko -->

Clear cache and empty generated directory.

1
<type name="Magento\Checkout\Model\DefaultConfigProvider">
        <plugin name="add_product_datails"
         type="NameSpace\ModuleName\Plugin\Model\DefaultConfigProvider"/>
    </type>


 public function afterGetConfig(\Magento\Checkout\Model\DefaultConfigProvider $subject, $result)
{ 
      $items = $result['totalsData']['items'];
    foreach ($items as $index => $item) {
   $result['quoteItemData'][$index]['your_mesage_key'] = $yourmessage;
}
}

into that summary.phtml

/vendor/magento/module-checkout/view/frontend/web/template/summary/item/details.html

<!-- ko if: (getMessage($parents[1]))-->
        <span class="product-view-approveditem-check-icon">
            <span class="label" data-bind="text: getMessage($parents[1]"></span>
        </span>
    <!-- /ko -->

details.js

getMessage: function (itemId) {
                var itemsData = window.checkoutConfig.quoteItemData;
                var msg= '';
                itemsData.forEach(function(item) {
                    if (item.item_id == itemId['item_id']) {
                        msg= item.is_show_icon;
                    }
                });
            return msg;
            },
  • its not working its still loading – divya sekar May 17 at 12:39

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.