Magento – How to reload the quote or cart in Magento 2

cartmagento2quotetotals

I am working on ajax cart.
When i update the cart qty, I am trying to reload the quote but its not loading properly

$this->cart->getQuote()->getShippingAddress()->collectShippingRates()->save();
$this->cart->getQuote()->setTotalsCollectedFlag(false);
$this->cart->getQuote()->collectTotals();
$this->cart->getQuote()->save(); 

How to reload the quote so that total calculation should happen properly
in magento1 we use cart::init()

Best Answer

I hope this may helps you

After add to cart run this code:

use Magento\Framework\Session\SessionManagerInterface;
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
use Magento\Framework\Stdlib\Cookie\PublicCookieMetadata;
use Magento\Framework\Stdlib\CookieManagerInterface;

in constructor:

    CookieManagerInterface $cookieManager,
    CookieMetadataFactory $cookieMetadataFactory,
    SessionManagerInterface $sessionManager,

After product was added to cart

$metadata = $this->_cookieMetadataFactory
        ->createPublicCookieMetadata()
        ->setPath('/');

$sectiondata = json_decode($this->_cookieManager->getCookie('section_data_ids'));

$sectiondata->cart += 1000;

$this->_cookieManager->setPublicCookie(
    'section_data_ids',
    json_encode($sectiondata),
    $metadata
);  

Before trying above code try with frontend/section.xml :

 <action name="NAMESPACE/MODULE/CARTUPDATE/">
     <section name="cart"/>
 </action>
Related Topic