0

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

1 Answer 1

1

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();
}
3
  • can you please tell me which observer/plugin will work to setGrandTotal() to order. Mar 17, 2020 at 10:46
  • checkout_cart_product_add_after try with this observer. Mar 17, 2020 at 10:52
  • i want to setGrandTotal() at order level. if i use checkout_cart_product_add_after it will trigger after adding each product to the cart. Mar 17, 2020 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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