Magento 2: How to Display Product Available Quantity on Checkout Order Summary

checkoutknockoutjsmagento2

I would like to know if there is any way, to show the Product(Added to cart) Available Qty in checkout page under Product's name in Order Summary Section in Magento 2.

As per my knowledge so far, I can see that, to show this section Magento 2 uses knockout JS(details.js inside magento module-checkout). And I have already extended that JS and it's HTML to show the Product SKU at the same place successfully. I got the product object from the quoteItems and there I can find the Product's SKU, but there is no information for the Product's Stock and QTY available.

Below is the image for the reference:

enter image description here

Best Answer

Try following way:

Create 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\DefaultConfigProvider">
        <plugin name="vendor_module_type_product_customer_data_default_item" type="Vendor\Module\Plugin\Checkout\Model\DefaultConfigProvider" sortOrder="1"/>
    </type>
</config>

And now create plugin:

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

namespace Vendor\Module\Plugin\Checkout\Model;

use Magento\Checkout\Model\Session as CheckoutSession;

class DefaultConfigProvider
{
    protected $stockItemRepository;
    protected $checkoutSession;

    public function __construct(
        CheckoutSession $checkoutSession,
        \Magento\CatalogInventory\Model\Stock\StockItemRepository $stockItemRepository
    ) {
        $this->checkoutSession = $checkoutSession;
        $this->stockItemRepository = $stockItemRepository;
    }

    /**
     * {@inheritdoc}
     */
    public function aroundGetConfig(
        \Magento\Checkout\Model\DefaultConfigProvider $subject,
        \Closure $proceed
    ) {
        $result = $proceed();

        if (isset($result['totalsData'])) {
            $totalsData = $result['totalsData'];

            if (isset($totalsData['items'])) {
                $items = $totalsData['items'];

                foreach ($items as &$item) {
                    $item['qty'] = $item['qty'].$this->getAvailableStock($item['item_id']);
                }

                $result['totalsData']['items']=$items;
            }
        }

        return $result;
    }

    public function getAvailableStock($item_id)
    {
        $quote = $this->checkoutSession->getQuote();
        $items = $quote->getAllItems();

        foreach ($items as $item) {
            if ($item->getId() == $item_id) {
                $productStock = $this->stockItemRepository->get($item->getProductId());
                if ($productStock->getQty()) {
                    $availableQty = $productStock->getQty() - $item->getQty();
                    return "  ".__("Available Qty: ") . $availableQty;
                }

                return '';
            }
        }

        return '';
    }

}