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

3 REPLIES 3

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.


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: Magento 2 + Cart Subtotal updated from controller method

Hello Sunil,

 

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

Re: Magento 2 + Cart Subtotal updated from controller method

To update the cart subtotal or grand total from a controller in Magento 2, you can use the following code:

<?php
namespace Vendor\Module\Controller\Index;

use Magento\Framework\App\Action\Context;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Quote\Api\CartRepositoryInterface;

class Index extends \Magento\Framework\App\Action\Action
{
    protected $checkoutSession;
    protected $cartRepository;

    public function __construct(
        Context $context,
        CheckoutSession $checkoutSession,
        CartRepositoryInterface $cartRepository
    ) {
        $this->checkoutSession = $checkoutSession;
        $this->cartRepository = $cartRepository;
        parent::__construct($context);
    }

    public function execute()
    {
        $post = $this->getRequest()->getPostValue();
        $quoteId = $this->checkoutSession->getQuoteId();
        $quote = $this->cartRepository->getActive($quoteId);

        $donationAmount = (float) $post['donation_amount'];
        $grandTotal = $quote->getGrandTotal();
        $newGrandTotal = $grandTotal + $donationAmount;

        $quote->setGrandTotal($newGrandTotal);
        $quote->setBaseGrandTotal($newGrandTotal);
        $quote->collectTotals();
        $this->cartRepository->save($quote);

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

Here, we are injecting two classes via constructor dependency injection: CheckoutSession and CartRepositoryInterface. We are getting the current quote using getActive() method of CartRepositoryInterface. Then we are calculating the new grand total after adding the donation amount, setting the new grand total using setGrandTotal() method, and calling collectTotals() method to recalculate all totals again. Finally, we are saving the updated quote using save() method of CartRepositoryInterface.

Note that we are also setting the base grand total to the same value as the grand total because both should have the same value.