4

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

  • I do have the same problem – Codrain Technolabs Pvt Ltd Apr 20 '17 at 14:01
  • @Milan Pattani how did you get sku in there? I tried but couldnt get the sku there – Joey Oct 5 '17 at 6:04
  • 1
    @Joey: Override vendor/magento/module-checkout/view/frontend/web/template/summary/item/details.html file in your extension and add <strong class="product-item-sku" data-bind="text: getSku($parent.item_id)"></strong>, where you want. Then override vendor/magento/module-checkout/view/frontend/web/js/view/summary/item/details.js file in your extension and add "getSku" function to it. – Milan Pattani Oct 6 '17 at 6:59
  • many many thanks for your help. could you help me with adding getsku function in details.js file if Im not troubling you – Joey Oct 6 '17 at 8:45
  • 1
    @Joey: I am glad to hear that. Just up vote the Que. if you think so ;) – Milan Pattani Oct 12 '17 at 10:23
1

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 '';
    }

}
  • Thanks a ton Sohel, will give it a try and will let you know with the outcome soon. – Milan Pattani Apr 20 '17 at 16:04
  • That did the trick Sohel, thank you very much for your help. However, I had to customize it bit to match it to my exact requirement. But, your answer helped me much. Thanks again. – Milan Pattani Apr 21 '17 at 8:54
  • @sohel - Do this work for all type of products? – Bijal Bhavsar Jun 5 '17 at 11:14
  • @BijalBhavsar, no this exact code will not work with all types of products but, it guided me to achieve it, these will need little change based on type of product's data you are looking for. Let's say for the configurable item "$item->getProductId()" will provide you the parent item ID and not the child one. – Milan Pattani Oct 3 '17 at 15:08

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.