2

I am trying to update the order's grand total via a controller.

Here is my controller's code:

<?php

namespace Company\Vendor\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;

class Index extends Action
{
    protected $_checkoutSession;

    /**
     * @var \Magento\Quote\Api\CartRepositoryInterface
     */
    protected $_quoteRepository;

    public function __construct(
        Context $context,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
    ) {
        parent::__construct($context);
        $this->_checkoutSession = $checkoutSession;
        $this->_quoteRepository = $quoteRepository;
    }

    public function execute()
    {

         $this->_quoteRepository->get($this->_checkoutSession->getQuoteId());

         $quote = $this->_checkoutSession->getQuote();

         $grand_total = $quote->getGrandTotal(); // It contains correct order grand total

         $new_grand_total = $grand_total + 100; // Adding 100 to order total

         $quote->setGrandTotal($new_grand_total);

         $quote->collectTotals();

         $this->_quoteRepository->save($quote);

         echo $quote->getGrandTotal();
    }
}

Above code is not updating the grand total.

2 Answers 2

1

Use collectTotals() method to re calculate totals

Try this

protected $_checkoutSession;

protected $quoteRepository;

public function __construct(
    ...
    \Magento\Checkout\Model\Session $checkoutSession,
    \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
    ...
) {
    $this->_checkoutSession = $checkoutSession;
    $this->quoteRepository = $quoteRepository;
}


public function execute()
{
    $this->quoteRepository->get($this->_checkoutSession->getQuoteId());
    $quote = $this->_checkoutSession->getQuote();

    $grandTotal = "200"; // Set your grand total
    $this->_checkoutSession->getQuote()->setGrandTotal($grandTotal);
    $this->quoteRepository->save($quote->collectTotals());

    echo $quote->getGrandTotal();
}
5
  • Let's assume, Above code is worked, Than at cart page new grand total will display OR I need to create JS and layout files to update it ??
    – anonymous
    Commented Apr 9, 2018 at 7:24
  • Thank you for the answer @Prince but it is not working. It is displaying the original grand total only.
    – vedu
    Commented Apr 9, 2018 at 7:36
  • how do i override grand total? not sure where to start from Commented Aug 2, 2019 at 12:53
  • Yes it's not working Commented Feb 29, 2020 at 6:33
  • can i update total in admin, i have a input box to enter discount and apply discount Commented Feb 16, 2021 at 15:01
1

You cannot update grand total, in such way. In magento, grand total is basically depends on cart item price and discount and tax calculation etc. So you cannot update the grand total.

I guess that you want implement concept like Extra Fee, Then you can extension like:

https://github.com/magecomp/magento2-extra-fee

https://github.com/sivajik34/Custom-Fee-Magento2

etc.

3
  • Yes, you are right. I want to add some extra fee. That extra fee should be added on the checkout page, payment step, depending on the checkbox is checked or not. Any hint for this?
    – vedu
    Commented Apr 9, 2018 at 7:02
  • @vedu If you are success on adding extra fee based on checkbox is checked then please share your code. I need to do same. Thanks in advance.. Commented Apr 10, 2019 at 12:53
  • @Bharat, have you done the above scenaria. I need the same Commented May 27, 2021 at 6:11

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.