Magento 2 how to get product name and its qty in mini cart when we add to mini cart
Magento 2 – How to Get Product Name and Qty in Mini Cart
magento2mini-cart
Related Solutions
That is magento2's default bug
https://github.com/magento/magento2/issues/5377
https://github.com/magento/magento2/issues/4170
for the temporary solution, you can refresh mini cart in all page for that you can use below code.
default.xml
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<block class="Magento\Framework\View\Element\Template" name="refresh.minicart" template="{vendor}_{module}::refreshMinicart.phtml" />
</referenceContainer>
</body>
</page>
refreshMinicart.phtml
<script>
require([
'Magento_Customer/js/customer-data'
], function (customerData) {
var sections = ['cart'];
customerData.invalidate(sections);
});
</script>
EDIT
/app/design/frontend/Magento/luma/Magento_Theme/layout/default.xml
/app/design/frontend/Magento/luma/Magento_Theme/templates/refreshMinicart.phtml
or if you have created module then you can paste at
app/code/Vendor/Module/view/frontend/layout/
app/code/Vendor/Module/view/frontend/templates/
I am now running into this same issue. It appears to be related to the way the cart is cached in the browser's "local storage". I don't actually have a solution yet, unfortunately, but clearing the local storage in Chome refreshed my minicart.
There are some open bugs in Magento's GitHub:
It looks like a long standing bug. Hopefully they will fix it soon.
Here are some related SE questions I'm working from right now:
- How to trigger a minicart update after adding to cart
- Magento 2: Syncing Backend and Frontend State/Cache
My (current) solution is to request that the minicart component refresh by default on page load. This way if the user updates the cart via an API request somewhere else, when they reload the Magento site the cart refreshes.
Specifically, I override the minicart.js
file and modified it to force the customerData
to refresh in the initialize
method every page load:
initialize: function () {
var self = this,
cartData = customerData.get('cart');
....
// always clear minicart local storage cache, since we might be updating it via API call
customerData.reload(['cart'], false);
// if (cartData().website_id !== window.checkout.websiteId) {
// customerData.reload(['cart'], false);
// }
return this._super();
},
Best Answer
Try to use this code :
Factory Method :
Object Manager Method :
It will helpful for you.