Display “Not Visible Individually” Product Into Mini Cart In Magento 2 – Here I am going to explain you how to display “not visible individually” products into mini cart in your custom module.To display “Not Visible Individually” products into mini cart you need to follow following steps-
Step-I
Set preference for “Magento\Checkout\CustomerData\Cart” in di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <preference for="Magento\Checkout\CustomerData\Cart" type="Webkul\TestModule\CustomerData\Cart" /> </config>
Step-II
Make a Cart.php file on location Webkul\TestModule\CustomerData then copy all the codes of Magento\Checkout\CustomerData\Cart.php to your custom module file Webkul\TestModule\CustomerData\Cart.php
Step-III
Edit in method getRecentItems()
protected function getRecentItems() { $items = []; if (!$this->getSummaryCount()) { return $items; } foreach (array_reverse($this->getAllQuoteItems()) as $item) { /* @var $item \Magento\Quote\Model\Quote\Item */ if (!$item->getProduct()->isVisibleInSiteVisibility()) { $product = $item->getOptionByCode('product_type') !== null ? $item->getOptionByCode('product_type')->getProduct() : $item->getProduct(); $products = $this->catalogUrl->getRewriteByProductStore([$product->getId() => $item->getStoreId()]); if (!isset($products[$product->getId()])) { if ($product->getId() === 4){ // set your condition $items[] = $this->itemPoolInterface->getItemData($item); // "not visible individually" product having id 4 added into items array } continue; } $urlDataObject = new \Magento\Framework\DataObject($products[$product->getId()]); $item->getProduct()->setUrlDataObject($urlDataObject); } $items[] = $this->itemPoolInterface->getItemData($item); } return $items; }
In this way you can display “not visible individually” products into mini cart.
4 comments
The actual location of class ItemPoolInterface is MagentoCheckoutCustomerDataItemPoolInterface
So, you will have to write “MagentoCheckoutCustomerDataItemPoolInterface” instead of “ItemPoolInterface” then it will work. With Regards, Rani (Webkul Team)
After I put this thing \Magento\Checkout\CustomerData\ItemPoolInterface on use and construct still its not working.
Can you please help me to fix it.