Question

Kindly give me an example about how to set GrandTotal via the controller in magento 2.3. I am new to Magento.

Was it helpful?

Solution

Please try with below code in controller :

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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top