Hi to all!
Looking in the Internet there are two way of getting the Quote total on the checkout page.
I use Checkout\Model\Session, the get the Quote and its total.
The other way is to use Checkout\Model\Cart model.
Are there any cons or pros to prefer one or another of those models?
Regards!
Hello @mpstoevgmaea24
To get the total amount on the checkout page in Magento 2, you can use JavaScript or PHP, Here’s how to achieve it in both ways:
Using JavaScript Concept:
require(['Magento_Checkout/js/model/quote'], function (quote) { // Get total amount var totalAmount = quote.getTotals(); console.log('Total Amount: ', totalAmount); });
<head> <script src="Vendor_ModuleName::js/custom.js"/> </head>
Using PHP Concept: If you need to retrieve the total amount from a block or controller, you can use the following approach:
namespace Vendor\ModuleName\Block; use Magento\Framework\View\Element\Template; use Magento\Checkout\Model\Session as CheckoutSession; class CheckoutAmount extends Template { protected $checkoutSession; public function __construct( Template\Context $context, CheckoutSession $checkoutSession, array $data = [] ) { $this->checkoutSession = $checkoutSession; parent::__construct($context, $data); } public function getTotalAmount() { $quote = $this->checkoutSession->getQuote(); return $quote->getGrandTotal(); // This returns the total amount } }
<div>Total Amount: <?= $block->getTotalAmount() ?></div>
Hope it helps !
If you find our reply helpful, please give us kudos.
A Leading Magento Development Agency That Delivers Powerful Results, Innovation, and Secure Digital Transformation.
WebDesk Solution Support Team
Get a Free Quote | | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789
Thank You,
WebDesk Solution Support Team
Get a Free Quote | Email | Adobe Commerce Partner | Hire Us | Call Us 877.536.3789
Location: 150 King St. W. Toronto, ON M5H 1J9