Updating Grand Total in Magento Cart Page
As a seasoned developer working on e-commerce platforms like Magento, I’ve encountered many situations where customers require custom functionality to enhance their shopping experience. One common request is the ability for customers to apply account points against the grand total during checkout.
In this post, we’ll address how to update the grand total in the cart page of Magento 2.3 when a customer redeems their account points.
Understanding Grand Total Calculation
The grand_total property in Magento represents the final price that includes tax and shipping costs. It is calculated based on the subtotal and additional fees, adjusted by any applied discounts or taxes.
You can retrieve the current grand total using:
$cartQuote = $this->cart->getQuote();
error_log($cartQuote->getGrandTotal());
However, simply setting a new value for grand_total won’t update it in the cart:
$grandTotal = "200";
$cartQuote->setGrandTotal($grandTotal);
$this->quoteRepository->save($cartQuote);
The issue lies in how Magento calculates and stores grand total. It’s not a simple property that can be updated directly.
Updating Grand Total via Quote Calculation
To update the grand total, you need to recalculate the quote with the new information. Here are the steps:
- Retrieve the current quote.
- Adjust the necessary values (like subtotal or discounts).
- Recollect the quote to recalculate and apply the changes.
Here’s an example of how to subtract a specific amount from the grand total:
$cartQuote = $this->quoteRepository->getActiveQuote();
$subtotalToSubtract = 200; // Amount in rupees
// Subtract the specified amount from subtotal
$cartQuote->setSubtotal($cartQuote->getSubtotal() - $subtotalToSubtract);
// Recollect the quote to recalculate grand total
$this->checkoutSession->setQuoteId($cartQuote->getId());
$this->quoteRepository->save($cartQuote);
In this code, we first retrieve the active quote. We then calculate the new subtotal by subtracting the desired amount from the current value.
After adjusting the subtotal, we call $this->quoteRepository->save($cartQuote); to trigger a recalculation of the grand total based on the updated values.
Integrating with Account Points System
To apply account points as discounts, you can create an observer that listens for changes in the quote and adjusts the subtotal accordingly. This involves creating a custom module and configuring it to interact with Magento’s event system.
You may also need to handle exceptions where the customer doesn’t have enough points or if the amount entered exceeds their available balance.
Remember, any code modification should be done carefully and tested thoroughly to ensure it doesn’t break other functionalities in your store.
Conclusion
Updating the grand total during checkout requires a deep understanding of Magento’s quote calculation process. Instead of directly setting the grand_total property, we need to adjust other values like subtotal or discounts that contribute to its calculation.
By following the steps outlined above and creating an observer for account points redemption, you can provide a seamless shopping experience for your customers while keeping your codebase maintainable and secure.
Visit Laravel Company to explore more Magento development tips and tricks.