3

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()

2
  • 1
    did you get any success on this? Nov 3, 2016 at 12:22
  • did you get any success on this? i am getting same problem Feb 15, 2018 at 8:42

2 Answers 2

0

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>
0

You can do this in Your JS

like this

define([
    'jquery',
    'Magento_Customer/js/customer-data'
], function($,customerData){
    return function (config){
        var ajaxUrl = config.ajaxUrl;
        $("#addtoCart").on("click",function (event){
            event.preventDefault();
            let param = {
                Sku : $("#productSku").val()
            };
            $.ajax({
                showLoader: true,
                url: ajaxUrl,
                data: param,
                type: "POST"
            }).done(function (data) {
                // you can add more section with comma seprated
                let cartSection = ['cart'];
                // if you want to invalidate previous data
                customerData.invalidate(sections);
                //Reload Secion
                customerData.reload(cartSection, true);
            });
        });
    }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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