0

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->truncate();
        $this->cart->addProduct($_product, $params);
        $this->cart->save();
0

4 Answers 4

1

Override this file

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

app\code\Namespace\ModuleName\CustomerData\Cart.php

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
                }
                continue;
            }
            $urlDataObject = new \Magento\Framework\DataObject($products[$product->getId()]);
            $item->getProduct()->setUrlDataObject($urlDataObject);
        }
        $items[] = $this->itemPoolInterface->getItemData($item);
    }
    return $items;
}
1

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.

    <sequence>
        <module name="Magento_Checkout" />
    </sequence>

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

Thanks

0

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"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd">
    <action name="your/ajax/controller">
        <section name="cart"/>
    </action>
</config>

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

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

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"/>
        </action>
    </config>

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

2
  • It does not work! 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 . Jan 15, 2018 at 10:15

Your Answer

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

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