In this blog, we are going to talk about how to check checkout cart page stock status in magento 2.
why? magento 2 default functionality not show stock status checkout cart page so described how to show status checkout cart page in below code.
- Create Folder: app/code/Vendor/Module
– Here Vendor is module namespace and Module is module’s name. - Create module.xml file in app/code/Vendor/Module/etc folder with the following code.
123456<config xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance” xsi:noNamespaceSchemaLocation=“urn:magento:framework:Module/etc/module.xsd”><module name=“Vendor_Module”><sequence><module name=“Magento_Checkout”/></sequence></module> - Create registration.php file in app/code/Vendor/Module folder to register the module with the following code.
1234567<?php\Magento\Framework\Component\ComponentRegistrar::register(\Magento\Framework\Component\ComponentRegistrar::MODULE,‘Vendor_Module’,__DIR__); - Create di.xml file in app/code/Vendor/Module/etc folder block override with the following code.
12345<?xml version=“1.0” encoding=“UTF-8”?><config xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”xsi:noNamespaceSchemaLocation=“urn:magento:framework:ObjectManager/etc/config.xsd”><preference for=“Magento\Checkout\Block\Cart\Item\Renderer”type=“Vendor\Module\Plugin\Checkout\Block\Cart\Item\Renderer” /> - Create Renderer.php file in app/code/Vendor/Module/Plugin/Checkout/Block/Cart/Item folder the following code.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162<?phpnamespace Vendor\Module\Plugin\Checkout\Block\Cart\Item;use Magento\Catalog\Model\Product\Configuration\Item\ItemResolverInterface;use Magento\CatalogInventory\Helper\Data;use Magento\CatalogInventory\Model\Stock;use Magento\Framework\Pricing\PriceCurrencyInterface;use Magento\Framework\View\Element\Message\InterpretationStrategyInterface;class Renderer extends \Magento\Checkout\Block\Cart\Item\Renderer{protected $stockRegistry;public function __construct(\Magento\Framework\View\Element\Template\Context $context,\Magento\Catalog\Helper\Product\Configuration $productConfig,\Magento\Checkout\Model\Session $checkoutSession,\Magento\Catalog\Block\Product\ImageBuilder $imageBuilder,\Magento\Framework\Url\Helper\Data $urlHelper,\Magento\Framework\Message\ManagerInterface $messageManager,PriceCurrencyInterface $priceCurrency, \Magento\Framework\Module\Manager $moduleManager,InterpretationStrategyInterface $messageInterpretationStrategy,array $data = [],ItemResolverInterface $itemResolver = null,\Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry){parent::__construct($context, $productConfig, $checkoutSession, $imageBuilder, $urlHelper, $messageManager, $priceCurrency, $moduleManager, $messageInterpretationStrategy, $data, $itemResolver);$this->stockRegistry = $stockRegistry;}public function isProductAvailable(){$quoteItem = $this->getItem();$product = $this->getProduct();/* @var \Magento\CatalogInventory\Api\Data\StockStatusInterface $stockStatus */$stockStatus = $this->stockRegistry->getStockStatus($product->getId(), $product->getStore()->getWebsiteId());/* @var \Magento\CatalogInventory\Api\Data\StockStatusInterface $parentStockStatus */$parentStockStatus = false;/*** Check if product in stock. For composite products check base (parent) item stock status*/if ($quoteItem->getParentItem()) {$product = $quoteItem->getParentItem()->getProduct();$parentStockStatus = $this->stockRegistry->getStockStatus($product->getId(),$product->getStore()->getWebsiteId());}if ($stockStatus) {if ($stockStatus->getStockStatus() === Stock::STOCK_OUT_OF_STOCK|| $parentStockStatus && $parentStockStatus->getStockStatus() == Stock::STOCK_OUT_OF_STOCK) {return false;} else {return true;}}}} - Create default.phtml file in app/design/frontend/Themename/Magento_checkout/templates/cart/item/ design template file in following code.
12345678910<?php if ($block->isProductAvailable()): ?><div class=“stock available col-lg-4” title=“<?php /* @escapeNotVerified */ echo __(‘Availability’) ?>“><i class=“fa fa-check-circle” aria–hidden=“true”></i><span><?php /* @escapeNotVerified */ echo __(‘In Stock’) ?></span></div><?php else: ?><div class=“stock unavailable col-lg-4” title=“<?php /* @escapeNotVerified */ echo __(‘Availability’) ?>“><span><?php /* @escapeNotVerified */ echo __(‘Out of Stock’) ?></span></div><?php endif; ?>
We hope our technical blog which looking is very helpful for you. If you have any queries, feel free to leave a comment below.
Leave a Reply