I have create a ajax call and added a product to cart programmatically. After that i have redirected to direct checkout page. Now mini cart shows similar to attached image. Point to be noted is cart display like below attached image if i add "Not Visible Individually" item in cart. However it works perfectly if product is set visible on catalog and search from admin.

How can it populate correctly ?

Attached Image:

enter image description here

Add to cart code:

        $productId = $this->getRequest()->getParam('id');
        $params = array(
            'form_key' => $this->formKey->getFormKey(),
            'product' => $productId,
            'qty' => 1        
        $_product = $this->product->load($productId);
        $this->cart->addProduct($_product, $params);

4 Answers 4


Override this file


<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="Namespace\ModuleName\CustomerData\Cart" />


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() === 10){ // set your condition 
                    $items[] = $this->itemPoolInterface->getItemData($item); // "not visible individually" product having id 10 added into items array
            $urlDataObject = new \Magento\Framework\DataObject($products[$product->getId()]);
        $items[] = $this->itemPoolInterface->getItemData($item);
    return $items;

I have found answer of this. I need to override the Cart.php file for showing not visible items in mini cart. Update di.xml to point my custom Cart.php and override the method "getRecentItems".

In addition to this, we need to define sequence of loading module in module.xml file, it was missing in the shared answer.

Add it in module.xml, inside module node.

        <module name="Magento_Checkout" />

Reference: Display of "Not visible individually" for a simple product in mini cart in Magento 2



Try to add the file sections.xml with the code below:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    <action name="your/ajax/controller">
        <section name="cart"/>

Push this file inside etc/frontend folder. Remove var/generation folder and try again.

  • It does not work! Commented Jan 15, 2018 at 10:13
  • Please provide your code that you use to add the item to cart!
    – Nero Phung
    Commented Jan 15, 2018 at 10:14

In my opinion cart section must be updated before redirection to checkout , So you can add following code in Vendor/module/etc/frontend/sections.xml .

 <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd">
        <action name="forntname/controller/action">
            <section name="cart"/>

And ajaxComplete function to redirect to checkout , also add necessary condition as per your requirement within the ajaxComplete function.

  • It does not work! Commented Jan 15, 2018 at 10:13
  • You can check console , and make sure section.xml load sucessfully after execution of your product update ajax request and then redirect . Commented Jan 15, 2018 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.