Reading list Switch to dark mode

    Display ‘Not Visible Individually’ Product Into Mini Cart In Magento 2

    Updated 27 February 2024

    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

    Searching for an experienced
    Magento 2 Company ?
    Find out More

    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;
        }
    Catalog Product Visibility
    Click on Checkout page.

    In this way you can display “not visible individually” products into mini cart.

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    4 comments

  • Alex
  • Timo Gundacker
    • Webkul Support
      • Rajan
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home