Magento – Magento 2 : How to Update Quote Total

magento-2.1quote

I am creating functionality for additional donation. I am trying to add donation amount in quote total but it will not reflect.

This my code portion of that part :

    <?php 

    namespace Yogesh\Donation\Controller\Index;
    class Index extends \Yogesh\Donation\Controller\Index
    {
      public function execute()
      {
         $post = $this->getRequest()->getPostValue();
         $quote = $this->checkoutSession->getQuote();

         $donation = $post['donation_amount'];
         $grand_total = $quote->getGrandTotal();
         $new_grand_total = $grand_total + $donation;
         $quote->setGrandTotal($new_grand_total);
         $quote->save();

         $this->_redirect('checkout/cart');

    }
}

?>

Addition of donation amount not display or reflect on front side. How can I update quote total ? Please any one have idea about that.

Thanks

Best Answer

You can update current checkout or quote total like this :

    namespace Yogesh\Donation\Controller\Index;
    class Index extends \Yogesh\Donation\Controller\Index
    {
      public function execute()
      {
         $post = $this->getRequest()->getPostValue();
         $quote = $this->checkoutSession->getQuote();

         $donation = $post['donation_amount'];
         $grand_total = $quote->getGrandTotal();
         $new_grand_total = $grand_total + $donation;
         $quote->setGrandTotal($new_grand_total);
         $quote->save();

         $this->checkoutSession->getQuote()->collectTotals()->save();

         $this->_redirect('checkout/cart');
    }
}
Related Topic