cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2 + Cart Subtotal updated from controller method

Magento 2 + Cart Subtotal updated from controller method

I am creating Donation functionality  in which i can add  donation amount in sub total from cart page but i am not able to alter sub total or base total from checkout session. below is my code 

 

<?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');

    }
}

?>

Any one have an idea how can i update Quote Total from controller.

 

Thanks in Adavance

Tags (2)
2 REPLIES

Re: Magento 2 + Cart Subtotal updated from controller method

This is the wrong way.

 

You need to either create new total type or you need to set product custom price.

 

let me know which way you want to do, so i can share some code.

Re: Magento 2 + Cart Subtotal updated from controller method

Hello Sunil,

 

Thanks for reply. i would like to create new total type.