cancel
Showing results for 
Search instead for 
Did you mean: 

Get total amount on the checkout page

Get total amount on the checkout page

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!

1 REPLY 1

Re: Get total amount on the checkout page

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:

  • Override the Checkout Component: You can create a custom module or override the necessary JS component. If you're using a custom theme, you can modify the checkout/index/index template.
  • Access the Total Amount: You can access the total amount in the checkout page using the quote object. Here's an example of how to do this in a custom JavaScript file:

        

require(['Magento_Checkout/js/model/quote'], function (quote) {
    // Get total amount
    var totalAmount = quote.getTotals();
    console.log('Total Amount: ', totalAmount);
});

 

  • Add the JavaScript to Your Checkout Page: To include your custom JavaScript, you can add it in your module or theme. For example, you can use default.xml to include your script:

 

    <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:

  • Inject the Quote Model: In your block or controller, inject the \Magento\Checkout\Model\Session class to access the current quote.

       

 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
    }
}
  • Use the Block in Your Template: Then, you can call the getTotalAmount() method in your template file to display 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