1

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.

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.

  • 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 Apr 9 '18 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.. – Bharat Chodvadiya Apr 10 '19 at 12:53
0

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();
}
  • 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 Apr 9 '18 at 7:24
  • Thank you for the answer @Prince but it is not working. It is displaying the original grand total only. – vedu Apr 9 '18 at 7:36
  • how do i override grand total? not sure where to start from – jibin george Aug 2 '19 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.