{"id":7385,"date":"2019-10-04T12:23:52","date_gmt":"2019-10-04T06:53:52","guid":{"rendered":"https:\/\/www.hiddentechies.com\/blog\/?p=7385"},"modified":"2019-10-04T15:48:52","modified_gmt":"2019-10-04T10:18:52","slug":"magento-2-get-subtotal-grand-total-in-checkout-page-using-knockout","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-get-subtotal-grand-total-in-checkout-page-using-knockout\/","title":{"rendered":"Magento 2 &#8211; How to Get Subtotal and Grand total in Checkout Page using Knockout JS"},"content":{"rendered":"<p>In this post I am going to explain how to get subtotal and grand total in checkout page using knockout js in Magento 2.<\/p>\n<p>Here, I am using my custom module &#8211; Ht_Mymodule<\/p>\n<p>In order to display content on checkout page, first we need to add block in checkout_index_index.xml file.<\/p>\n<p>So create layout file checkout_index_index.xml in your module at below path and add below code.<\/p>\n<p>File Path: app\/code\/Ht\/Mymodule\/view\/frontend\/layout\/checkout_index_index.xml<\/p>\n<pre class=\"lang:default decode:true \">&lt;?xml version=\"1.0\"?&gt;\r\n\r\n&lt;page xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xsi:noNamespaceSchemaLocation=\"urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd\"&gt;\r\n    &lt;body&gt;\r\n        &lt;referenceContainer name=\"content\"&gt;\r\n            &lt;block class=\"Ht\\Mymodule\\Block\\Checkout\\Quoteinfo\" before=\"-\" cacheable=\"false\" template=\"Ht_Mymodule::checkout\/quoteinfo.phtml\"&gt;\r\n                &lt;arguments&gt;\r\n                    &lt;argument name=\"jsLayout\" xsi:type=\"array\"&gt;\r\n                        &lt;item name=\"components\" xsi:type=\"array\"&gt;\r\n                            &lt;item name=\"checkout-quote-information\" xsi:type=\"array\"&gt;\r\n                                &lt;item name=\"component\" xsi:type=\"string\"&gt;Ht_Mymodule\/js\/view\/checkout\/quoteinfo&lt;\/item&gt;\r\n                                &lt;item name=\"config\" xsi:type=\"array\"&gt;\r\n                                    &lt;item name=\"template\" xsi:type=\"string\"&gt;Ht_Mymodule\/checkout\/quoteinfo&lt;\/item&gt;\r\n                                &lt;\/item&gt;\r\n                            &lt;\/item&gt;\r\n                        &lt;\/item&gt;\r\n                    &lt;\/argument&gt;\r\n                &lt;\/arguments&gt;\r\n            &lt;\/block&gt;\r\n        &lt;\/referenceContainer&gt;\r\n    &lt;\/body&gt;\r\n&lt;\/page&gt;<\/pre>\n<p>In this file you can see:<\/p>\n<p>\u2013 Block class: Ht\\Mymodule\\Block\\Checkout\\Quoteinfo<br \/>\n\u2013 Block template: Ht_Mymodule::checkout\/quoteinfo.phtml<br \/>\n\u2013 Knockout js file: Ht_Mymodule\/js\/view\/checkout\/quoteinfo.js<br \/>\n\u2013 The template file of knockout js: Ht_Mymodule\/checkout\/quoteinfo.html<\/p>\n<p>Now we are going to create all above files.<\/p>\n<p>1. Block class: Quoteinfo.php<\/p>\n<p>File Path: app\/code\/Ht\/Mymodule\/Block\/Checkout\/Quoteinfo.php<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php\r\n\r\nnamespace Ht\\Mymodule\\Block\\Checkout;\r\n\r\nclass Quoteinfo extends \\Magento\\Framework\\View\\Element\\Template \r\n{\r\n    public function __construct(\r\n        \\Magento\\Framework\\View\\Element\\Template\\Context $context, \r\n        array $layoutProcessors = [], \r\n        array $data = []\r\n    )\r\n    {\r\n\r\n        parent::__construct($context, $data);\r\n        $this-&gt;layoutProcessors = $layoutProcessors;\r\n    }\r\n    \r\n    public function getJsLayout() \r\n    {\r\n        foreach ($this-&gt;layoutProcessors as $processor) {\r\n\r\n            $this-&gt;jsLayout = $processor-&gt;process($this-&gt;jsLayout);\r\n        }\r\n        return parent::getJsLayout();\r\n    }\r\n}<\/pre>\n<p>2. Block template: quoteinfo.phtml<\/p>\n<p>File Path: app\/code\/Ht\/Mymodule\/view\/frontend\/templates\/checkout\/quoteinfo.phtml<\/p>\n<pre class=\"lang:default decode:true \">&lt;div id=\"customer-page\"&gt;\r\n    &lt;div id=\"block-quote-info\" data-bind=\"scope:'checkout-quote-information'\" class=\"block\"&gt;\r\n        &lt;!-- ko template: getTemplate() --&gt;&lt;!-- \/ko --&gt;\r\n        &lt;script type=\"text\/x-magento-init\"&gt;\r\n            {\r\n                \"#block-quote-info\": {\r\n                    \"Magento_Ui\/js\/core\/app\": &lt;?php \/* @escapeNotVerified *\/ echo $block-&gt;getJsLayout(); ?&gt;\r\n                }\r\n            }\r\n        &lt;\/script&gt;\r\n    &lt;\/div&gt;\r\n&lt;\/div&gt;<\/pre>\n<p>3. Knockout js file: quoteinfo.js<\/p>\n<p>File Path: app\/code\/Ht\/Mymodule\/view\/frontend\/web\/js\/view\/checkout\/quoteinfo.js<\/p>\n<pre class=\"lang:default decode:true \">define([\r\n    'jquery',\r\n    'ko',\r\n    'uiComponent',\r\n    'Magento_Checkout\/js\/model\/quote'\r\n], function($, ko, Component, quote) {\r\n    \"use strict\";\r\n\r\n    return Component.extend({\r\n        getSubtotal: function() {\r\n            var totals = quote.totals();\r\n            return (totals ? totals : quote)['subtotal'];\r\n        },\r\n        getGrandTotal: function() {\r\n            var totals = quote.totals();\r\n            return (totals ? totals : quote)['grand_total'];\r\n        }\r\n    });\r\n});<\/pre>\n<p>4. The template file of knockout js: quoteinfo.html<\/p>\n<p>File Path: app\/code\/Ht\/Mymodule\/view\/frontend\/web\/template\/checkout\/quoteinfo.html<\/p>\n<pre class=\"lang:default decode:true \">&lt;div class=\"quote-info\"&gt;\r\n    &lt;div class=\"quote-subtotal\"&gt;\r\n        &lt;label data-bind=\"text: 'Subtotal: '\"&gt;&lt;\/label&gt;\r\n        &lt;span data-bind=\"text: getSubtotal()\"&gt;&lt;\/span&gt;\r\n    &lt;\/div&gt;\r\n    &lt;div class=\"quote-grandtotal\"&gt;\r\n        &lt;label data-bind=\"text: 'Grand Total: '\"&gt;&lt;\/label&gt;\r\n        &lt;span data-bind=\"text: getGrandTotal()\"&gt;&lt;\/span&gt;\r\n    &lt;\/div&gt;\r\n&lt;\/div&gt;<\/pre>\n<p>Now, Run upgrade and deploy commands and check for the result on checkout page.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post I am going to explain how to get subtotal and grand total in checkout page using knockout js in Magento 2. Here, I am using my custom module &#8211; Ht_Mymodule In order to display content on checkout page, first we need to add block in checkout_index_index.xml file. So create layout file checkout_index_index.xml&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-get-subtotal-grand-total-in-checkout-page-using-knockout\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":7414,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[10,27],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7385"}],"collection":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/comments?post=7385"}],"version-history":[{"count":1,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7385\/revisions"}],"predecessor-version":[{"id":7386,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7385\/revisions\/7386"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/7414"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=7385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=7385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=7385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}