3

enter image description hereAfter controller_action_predispatch_checkout_cart_index event dispatcher i am trying to update quote in cart page.All working fine except price.The price is not getting updated quote and quoteitem database table and in summary also.Can anyone help me.

<?php
namespace Vender\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
class PredispatchCheckoutCart implements ObserverInterface{

    public function __construct(
        \Magento\Framework\ObjectManagerInterface $_objectManager,
        \Magento\Framework\App\Request\Http $_request,
        \Magento\Framework\Message\ManagerInterface $_messangeManager,
        \Magento\Checkout\Helper\Cart $_cartHelper
    ) {
        $this->_objectManager = $_objectManager;
        $this->_request = $_request;
        $this->_messangeManager = $_messangeManager;
        $this->_cartHelper = $_cartHelper;
    }

    public function execute(\Magento\Framework\Event\Observer $observer){

            $quoteId = intval($this->_request->getParam('quoteId'));  
            $cartsess = $this->_objectManager->get('Magento\Checkout\Model\Session');
            $cartsess->setQuoteId($quoteId);
            $cartsess->setLoadInactive(true);
            $quote = $cartsess->getQuote();
            $quote->setIsActive(true);
            $quote->save();
            $quote->setTotalsCollectedFlag(false)->collectTotals();
            $this->_messangeManager->addSuccess('Product is successfully added in to cart.');   
            $redirectUrl = $this->_cartHelper->getCartUrl();
            $observer->getControllerAction()->getResponse()->setRedirect($redirectUrl);

        }
    }
}

2 Answers 2

3

For Recalculate total below code works for me.

$quote->setTriggerRecollect(1); //Magic line.
$quote->collectTotals();
$quote->save();
2

May you forgot to save the collect total. We can try

$quote->setTotalsCollectedFlag(false)->collectTotals()->save()

or

$cartsess->getQuote()->collectTotals()->save()
0

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.