Magento 2 Get Cart Quote Total in minicart.phtmlMagento 2: to use or not to use the ObjectManager directly?Magento 2 : Show cart items total to header cart linkUpdate Minicart programatically in magento 2problem unsetting quote (cart), data is not savedMagento update quote item change row totalHow do I get the discount amount in the minicart for 1.9?AJAX cart displaying parent image instead of product imageMagento 2 - Update Cart Block with Custom Discount ValueMagento2: How to show subtotal in minicartCoupon block not displaying inside mini cartMagento 2 Mini Cart values with Full Page CacheUpdate the Price of an item when adding to the cartCart details not getting in mini cart

Extract more than nine arguments that occur periodically in a sentence to use in macros in order to typset

Electoral considerations aside, what are potential benefits, for the US, of policy changes proposed by the tweet recognizing Golan annexation?

Plot of a tornado-shaped surface

How to explain what's wrong with this application of the chain rule?

Does IPv6 have similar concept of network mask?

Creepy dinosaur pc game identification

What are some good ways to treat frozen vegetables such that they behave like fresh vegetables when stir frying them?

Are Captain Marvel's powers affected by Thanos' actions in Infinity War

Can I still be respawned if I die by falling off the map?

Multiplicative persistence

Is there a RAID 0 Equivalent for RAM?

Is there a way to get `mathscr' with lower case letters in pdfLaTeX?

Is there an injective, monotonically increasing, strictly concave function from the reals, to the reals?

How could a planet have erratic days?

Non-trope happy ending?

Why does the Sun have different day lengths, but not the gas giants?

Does the UK parliament need to pass secondary legislation to accept the Article 50 extension

Calculating total slots

What is the evidence for the "tyranny of the majority problem" in a direct democracy context?

Limits and Infinite Integration by Parts

How does the math work for Perception checks?

What should you do when eye contact makes your subordinate uncomfortable?

How do apertures which seem too large to physically fit work?

Pre-mixing cryogenic fuels and using only one fuel tank



Magento 2 Get Cart Quote Total in minicart.phtml


Magento 2: to use or not to use the ObjectManager directly?Magento 2 : Show cart items total to header cart linkUpdate Minicart programatically in magento 2problem unsetting quote (cart), data is not savedMagento update quote item change row totalHow do I get the discount amount in the minicart for 1.9?AJAX cart displaying parent image instead of product imageMagento 2 - Update Cart Block with Custom Discount ValueMagento2: How to show subtotal in minicartCoupon block not displaying inside mini cartMagento 2 Mini Cart values with Full Page CacheUpdate the Price of an item when adding to the cartCart details not getting in mini cart













7















I'm trying to get the cart quote total in minicart.phtml, but I'm having no luck. I'm injecting MagentoCheckoutModelCart.



Here's my code:



$this->cart = $cart;
$cartQuote= $this->cart->getQuote()->getData();
echo $cartQuote['base_grand_total'];


After I run that code the minicart breaks and disappears totally from the frontend.



Thanks!










share|improve this question






















  • Can you share more information?

    – Sohel Rana
    Jun 16 '16 at 18:32











  • Hei. Seems not to work after update to 2.1 it only returns data when in cart/checkout page, other pages are returning 0 values.

    – Frii Zuurikas
    Aug 29 '16 at 11:57















7















I'm trying to get the cart quote total in minicart.phtml, but I'm having no luck. I'm injecting MagentoCheckoutModelCart.



Here's my code:



$this->cart = $cart;
$cartQuote= $this->cart->getQuote()->getData();
echo $cartQuote['base_grand_total'];


After I run that code the minicart breaks and disappears totally from the frontend.



Thanks!










share|improve this question






















  • Can you share more information?

    – Sohel Rana
    Jun 16 '16 at 18:32











  • Hei. Seems not to work after update to 2.1 it only returns data when in cart/checkout page, other pages are returning 0 values.

    – Frii Zuurikas
    Aug 29 '16 at 11:57













7












7








7


1






I'm trying to get the cart quote total in minicart.phtml, but I'm having no luck. I'm injecting MagentoCheckoutModelCart.



Here's my code:



$this->cart = $cart;
$cartQuote= $this->cart->getQuote()->getData();
echo $cartQuote['base_grand_total'];


After I run that code the minicart breaks and disappears totally from the frontend.



Thanks!










share|improve this question














I'm trying to get the cart quote total in minicart.phtml, but I'm having no luck. I'm injecting MagentoCheckoutModelCart.



Here's my code:



$this->cart = $cart;
$cartQuote= $this->cart->getQuote()->getData();
echo $cartQuote['base_grand_total'];


After I run that code the minicart breaks and disappears totally from the frontend.



Thanks!







magento2 cart phtml mini-cart






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jun 16 '16 at 17:37









PaulPaul

62831225




62831225












  • Can you share more information?

    – Sohel Rana
    Jun 16 '16 at 18:32











  • Hei. Seems not to work after update to 2.1 it only returns data when in cart/checkout page, other pages are returning 0 values.

    – Frii Zuurikas
    Aug 29 '16 at 11:57

















  • Can you share more information?

    – Sohel Rana
    Jun 16 '16 at 18:32











  • Hei. Seems not to work after update to 2.1 it only returns data when in cart/checkout page, other pages are returning 0 values.

    – Frii Zuurikas
    Aug 29 '16 at 11:57
















Can you share more information?

– Sohel Rana
Jun 16 '16 at 18:32





Can you share more information?

– Sohel Rana
Jun 16 '16 at 18:32













Hei. Seems not to work after update to 2.1 it only returns data when in cart/checkout page, other pages are returning 0 values.

– Frii Zuurikas
Aug 29 '16 at 11:57





Hei. Seems not to work after update to 2.1 it only returns data when in cart/checkout page, other pages are returning 0 values.

– Frii Zuurikas
Aug 29 '16 at 11:57










4 Answers
4






active

oldest

votes


















16















You have to just keep below line in your minicart.phtml file to get
updated subtotal ,




This Below line is workig for all cases if cache is enable its working fine,



<span data-bind="html: getCartParam('subtotal')"></span> 


For getting value of grandtotal,shipping rate,



You can get GrandTotal, subtotal and shipping rate for current quote using below code in minicart.phtml file, But when cache is enable at that time not update price when you add new product using below method.



<?php
$quote = $block->getTotalsCache();
$getSubTotal = $quote['subtotal']->getData('value');
$getGrandTotal = $quote['grand_total']->getData('value');
$getShippingRate = $quote['shipping']->getData('value');

$finalSubTotal = $this->helper('MagentoFrameworkPricingHelperData')->currency(number_format($getSubTotal,2),true,false);
$finalShippingTotal = $this->helper('MagentoFrameworkPricingHelperData')->currency(number_format($getShippingRate,2),true,false);
$finalGrandTotal = $this->helper('MagentoFrameworkPricingHelperData')->currency(number_format($getGrandTotal,2),true,false);
?>





share|improve this answer

























  • It worked perfectly on my localhost xamp on ubuntu, but $quote = $block->getTotalsCache(); not working on test server linux fedora

    – Kumar A.
    Sep 14 '16 at 9:28











  • Its already working on linux server, i think you have some conflict or some other error.

    – Rakesh Jesadiya
    Sep 14 '16 at 9:29











  • I got more involved and find $block->getTotalsCache(); works only when cache is disabled. Have you checked this with Cache Enabled? Mine is Magento2.1.0

    – Kumar A.
    Sep 14 '16 at 10:44







  • 1





    How to get discount amount like this?

    – Deeban Babu
    Sep 28 '16 at 12:48











  • @KumarAbhinav, please keep <span data-bind="html: getCartParam('subtotal')"></span> to get dynamic price after cache enable.

    – Rakesh Jesadiya
    Dec 7 '16 at 12:14


















7














We had a similar question from a client. where he wanted to show "[quantity] item [subtotal]" in a styled cart block instead of the default cart icon in the minicart.



We found this question here but didn't like the answer where we needed to extend the MagentoCheckoutCustomerDataCart class just to render some html correctly



this is the code how we fixed it in the template:



<span class="counter-label">
<!-- ko if: getCartParam('summary_count') == 1 -->
<!-- ko text: getCartParam('summary_count') --><!-- /ko -->
<!-- ko i18n: 'item' --><!-- /ko -->
<span data-bind="html: getCartParam('subtotal')"></span>
<!-- /ko -->
<!-- ko if: getCartParam('summary_count') != 1 -->
<!-- ko text: getCartParam('summary_count') --><!-- /ko -->
<!-- ko i18n: 'items' --><!-- /ko -->
<span data-bind="html: getCartParam('subtotal')"></span>
<!-- /ko -->
</span>


It seems like you can also use the standard knockout.js data binding and don't persé need to use the crazy magento 2 knockout comment method. this solved the problem where rendering the getCartParam('subtotal') with the html method where it would normally print the subtotal incorrectly due to the <span ="price"></span> tag






share|improve this answer
































    4














    This above code will works on the page load, but will not work with magento2 ajax add to cart as it uses Knockout JS now.



    For that you should use -



    1. Override the magento class "MagentoCheckoutCustomerDataCart" in your module and extend method "getSectionData"



    public function getSectionData()

    $objectManager = MagentoFrameworkAppObjectManager::getInstance(); // Instance of Object Manager
    $priceHelper = $objectManager->create('MagentoFrameworkPricingHelperData'); // Instance of Pricing Helper

    $totals = $this->getQuote()->getTotals();
    return [
    'summary_count' => $this->getSummaryCount(),
    'subtotal' => isset($totals['subtotal'])
    ? $this->checkoutHelper->formatPrice($totals['subtotal']->getValue())
    : 0,
    'subtotal_value' => isset($totals['subtotal'])
    ? $priceHelper->currency($totals['subtotal']->getValue(),true,false)
    : '',
    'possible_onepage_checkout' => $this->isPossibleOnepageCheckout(),
    'items' => $this->getRecentItems(),
    'extra_actions' => $this->layout->createBlock('MagentoCatalogBlockShortcutButtons')->toHtml(),
    'isGuestCheckoutAllowed' => $this->isGuestCheckoutAllowed(),
    ];



    Here I have added a new cart param "subtotal_value" as the "subtotal" will return the price container span and it will display as TEXT using KO.
    Here you have to use "Object Manager Instance" directly, as you wont be able to inject dependencies to the "__construct".



    NOTE, there are few exception where we might need to use "Object Manager Instance" directly. In our case it is backward compatibility of constructor.

    ObjectManager Exception




    1. Next, copy magento default theme "/cart/minicart.phtml" to your theme and add the KO codes.



      ko text: getCartParam('subtotal_value')







    share|improve this answer
































      0














      Define a block in your layout in which class marked as "MagentoCheckoutBlockCartTotals"



      <block class="MagentoCheckoutBlockCartTotals" name="quote.print.totals" as="quote.print.totals" after="checkout.cart" 
      template="MyNamespace_PrintCart::totals.phtml"/>


      Then in .phtml you can have below code



      <?php 
      $totals = $block->getTotals() ;
      ?>
      <table class="data table totals">
      <tbody>
      <?php foreach($totals as $key => $total) :?>
      <?php if(!empty($total->getValue())) :?>
      <tr>
      <td><?= $total->getTitle()->getText() ?></th>
      <td>
      <span class="price"><?= $this->helper('MagentoFrameworkPricingHelperData')->currency(number_format($total->getValue(),2),true,false) ?></span>
      </td>
      </tr>
      <?php endif ?>
      <?php endforeach ?>
      </tbody>
      </table>


      Expected Output



      enter image description here






      share|improve this answer






















        Your Answer








        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "479"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: false,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f121271%2fmagento-2-get-cart-quote-total-in-minicart-phtml%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        16















        You have to just keep below line in your minicart.phtml file to get
        updated subtotal ,




        This Below line is workig for all cases if cache is enable its working fine,



        <span data-bind="html: getCartParam('subtotal')"></span> 


        For getting value of grandtotal,shipping rate,



        You can get GrandTotal, subtotal and shipping rate for current quote using below code in minicart.phtml file, But when cache is enable at that time not update price when you add new product using below method.



        <?php
        $quote = $block->getTotalsCache();
        $getSubTotal = $quote['subtotal']->getData('value');
        $getGrandTotal = $quote['grand_total']->getData('value');
        $getShippingRate = $quote['shipping']->getData('value');

        $finalSubTotal = $this->helper('MagentoFrameworkPricingHelperData')->currency(number_format($getSubTotal,2),true,false);
        $finalShippingTotal = $this->helper('MagentoFrameworkPricingHelperData')->currency(number_format($getShippingRate,2),true,false);
        $finalGrandTotal = $this->helper('MagentoFrameworkPricingHelperData')->currency(number_format($getGrandTotal,2),true,false);
        ?>





        share|improve this answer

























        • It worked perfectly on my localhost xamp on ubuntu, but $quote = $block->getTotalsCache(); not working on test server linux fedora

          – Kumar A.
          Sep 14 '16 at 9:28











        • Its already working on linux server, i think you have some conflict or some other error.

          – Rakesh Jesadiya
          Sep 14 '16 at 9:29











        • I got more involved and find $block->getTotalsCache(); works only when cache is disabled. Have you checked this with Cache Enabled? Mine is Magento2.1.0

          – Kumar A.
          Sep 14 '16 at 10:44







        • 1





          How to get discount amount like this?

          – Deeban Babu
          Sep 28 '16 at 12:48











        • @KumarAbhinav, please keep <span data-bind="html: getCartParam('subtotal')"></span> to get dynamic price after cache enable.

          – Rakesh Jesadiya
          Dec 7 '16 at 12:14















        16















        You have to just keep below line in your minicart.phtml file to get
        updated subtotal ,




        This Below line is workig for all cases if cache is enable its working fine,



        <span data-bind="html: getCartParam('subtotal')"></span> 


        For getting value of grandtotal,shipping rate,



        You can get GrandTotal, subtotal and shipping rate for current quote using below code in minicart.phtml file, But when cache is enable at that time not update price when you add new product using below method.



        <?php
        $quote = $block->getTotalsCache();
        $getSubTotal = $quote['subtotal']->getData('value');
        $getGrandTotal = $quote['grand_total']->getData('value');
        $getShippingRate = $quote['shipping']->getData('value');

        $finalSubTotal = $this->helper('MagentoFrameworkPricingHelperData')->currency(number_format($getSubTotal,2),true,false);
        $finalShippingTotal = $this->helper('MagentoFrameworkPricingHelperData')->currency(number_format($getShippingRate,2),true,false);
        $finalGrandTotal = $this->helper('MagentoFrameworkPricingHelperData')->currency(number_format($getGrandTotal,2),true,false);
        ?>





        share|improve this answer

























        • It worked perfectly on my localhost xamp on ubuntu, but $quote = $block->getTotalsCache(); not working on test server linux fedora

          – Kumar A.
          Sep 14 '16 at 9:28











        • Its already working on linux server, i think you have some conflict or some other error.

          – Rakesh Jesadiya
          Sep 14 '16 at 9:29











        • I got more involved and find $block->getTotalsCache(); works only when cache is disabled. Have you checked this with Cache Enabled? Mine is Magento2.1.0

          – Kumar A.
          Sep 14 '16 at 10:44







        • 1





          How to get discount amount like this?

          – Deeban Babu
          Sep 28 '16 at 12:48











        • @KumarAbhinav, please keep <span data-bind="html: getCartParam('subtotal')"></span> to get dynamic price after cache enable.

          – Rakesh Jesadiya
          Dec 7 '16 at 12:14













        16












        16








        16








        You have to just keep below line in your minicart.phtml file to get
        updated subtotal ,




        This Below line is workig for all cases if cache is enable its working fine,



        <span data-bind="html: getCartParam('subtotal')"></span> 


        For getting value of grandtotal,shipping rate,



        You can get GrandTotal, subtotal and shipping rate for current quote using below code in minicart.phtml file, But when cache is enable at that time not update price when you add new product using below method.



        <?php
        $quote = $block->getTotalsCache();
        $getSubTotal = $quote['subtotal']->getData('value');
        $getGrandTotal = $quote['grand_total']->getData('value');
        $getShippingRate = $quote['shipping']->getData('value');

        $finalSubTotal = $this->helper('MagentoFrameworkPricingHelperData')->currency(number_format($getSubTotal,2),true,false);
        $finalShippingTotal = $this->helper('MagentoFrameworkPricingHelperData')->currency(number_format($getShippingRate,2),true,false);
        $finalGrandTotal = $this->helper('MagentoFrameworkPricingHelperData')->currency(number_format($getGrandTotal,2),true,false);
        ?>





        share|improve this answer
















        You have to just keep below line in your minicart.phtml file to get
        updated subtotal ,




        This Below line is workig for all cases if cache is enable its working fine,



        <span data-bind="html: getCartParam('subtotal')"></span> 


        For getting value of grandtotal,shipping rate,



        You can get GrandTotal, subtotal and shipping rate for current quote using below code in minicart.phtml file, But when cache is enable at that time not update price when you add new product using below method.



        <?php
        $quote = $block->getTotalsCache();
        $getSubTotal = $quote['subtotal']->getData('value');
        $getGrandTotal = $quote['grand_total']->getData('value');
        $getShippingRate = $quote['shipping']->getData('value');

        $finalSubTotal = $this->helper('MagentoFrameworkPricingHelperData')->currency(number_format($getSubTotal,2),true,false);
        $finalShippingTotal = $this->helper('MagentoFrameworkPricingHelperData')->currency(number_format($getShippingRate,2),true,false);
        $finalGrandTotal = $this->helper('MagentoFrameworkPricingHelperData')->currency(number_format($getGrandTotal,2),true,false);
        ?>






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 7 '16 at 12:22

























        answered Jun 17 '16 at 5:28









        Rakesh JesadiyaRakesh Jesadiya

        29.9k1576123




        29.9k1576123












        • It worked perfectly on my localhost xamp on ubuntu, but $quote = $block->getTotalsCache(); not working on test server linux fedora

          – Kumar A.
          Sep 14 '16 at 9:28











        • Its already working on linux server, i think you have some conflict or some other error.

          – Rakesh Jesadiya
          Sep 14 '16 at 9:29











        • I got more involved and find $block->getTotalsCache(); works only when cache is disabled. Have you checked this with Cache Enabled? Mine is Magento2.1.0

          – Kumar A.
          Sep 14 '16 at 10:44







        • 1





          How to get discount amount like this?

          – Deeban Babu
          Sep 28 '16 at 12:48











        • @KumarAbhinav, please keep <span data-bind="html: getCartParam('subtotal')"></span> to get dynamic price after cache enable.

          – Rakesh Jesadiya
          Dec 7 '16 at 12:14

















        • It worked perfectly on my localhost xamp on ubuntu, but $quote = $block->getTotalsCache(); not working on test server linux fedora

          – Kumar A.
          Sep 14 '16 at 9:28











        • Its already working on linux server, i think you have some conflict or some other error.

          – Rakesh Jesadiya
          Sep 14 '16 at 9:29











        • I got more involved and find $block->getTotalsCache(); works only when cache is disabled. Have you checked this with Cache Enabled? Mine is Magento2.1.0

          – Kumar A.
          Sep 14 '16 at 10:44







        • 1





          How to get discount amount like this?

          – Deeban Babu
          Sep 28 '16 at 12:48











        • @KumarAbhinav, please keep <span data-bind="html: getCartParam('subtotal')"></span> to get dynamic price after cache enable.

          – Rakesh Jesadiya
          Dec 7 '16 at 12:14
















        It worked perfectly on my localhost xamp on ubuntu, but $quote = $block->getTotalsCache(); not working on test server linux fedora

        – Kumar A.
        Sep 14 '16 at 9:28





        It worked perfectly on my localhost xamp on ubuntu, but $quote = $block->getTotalsCache(); not working on test server linux fedora

        – Kumar A.
        Sep 14 '16 at 9:28













        Its already working on linux server, i think you have some conflict or some other error.

        – Rakesh Jesadiya
        Sep 14 '16 at 9:29





        Its already working on linux server, i think you have some conflict or some other error.

        – Rakesh Jesadiya
        Sep 14 '16 at 9:29













        I got more involved and find $block->getTotalsCache(); works only when cache is disabled. Have you checked this with Cache Enabled? Mine is Magento2.1.0

        – Kumar A.
        Sep 14 '16 at 10:44






        I got more involved and find $block->getTotalsCache(); works only when cache is disabled. Have you checked this with Cache Enabled? Mine is Magento2.1.0

        – Kumar A.
        Sep 14 '16 at 10:44





        1




        1





        How to get discount amount like this?

        – Deeban Babu
        Sep 28 '16 at 12:48





        How to get discount amount like this?

        – Deeban Babu
        Sep 28 '16 at 12:48













        @KumarAbhinav, please keep <span data-bind="html: getCartParam('subtotal')"></span> to get dynamic price after cache enable.

        – Rakesh Jesadiya
        Dec 7 '16 at 12:14





        @KumarAbhinav, please keep <span data-bind="html: getCartParam('subtotal')"></span> to get dynamic price after cache enable.

        – Rakesh Jesadiya
        Dec 7 '16 at 12:14













        7














        We had a similar question from a client. where he wanted to show "[quantity] item [subtotal]" in a styled cart block instead of the default cart icon in the minicart.



        We found this question here but didn't like the answer where we needed to extend the MagentoCheckoutCustomerDataCart class just to render some html correctly



        this is the code how we fixed it in the template:



        <span class="counter-label">
        <!-- ko if: getCartParam('summary_count') == 1 -->
        <!-- ko text: getCartParam('summary_count') --><!-- /ko -->
        <!-- ko i18n: 'item' --><!-- /ko -->
        <span data-bind="html: getCartParam('subtotal')"></span>
        <!-- /ko -->
        <!-- ko if: getCartParam('summary_count') != 1 -->
        <!-- ko text: getCartParam('summary_count') --><!-- /ko -->
        <!-- ko i18n: 'items' --><!-- /ko -->
        <span data-bind="html: getCartParam('subtotal')"></span>
        <!-- /ko -->
        </span>


        It seems like you can also use the standard knockout.js data binding and don't persé need to use the crazy magento 2 knockout comment method. this solved the problem where rendering the getCartParam('subtotal') with the html method where it would normally print the subtotal incorrectly due to the <span ="price"></span> tag






        share|improve this answer





























          7














          We had a similar question from a client. where he wanted to show "[quantity] item [subtotal]" in a styled cart block instead of the default cart icon in the minicart.



          We found this question here but didn't like the answer where we needed to extend the MagentoCheckoutCustomerDataCart class just to render some html correctly



          this is the code how we fixed it in the template:



          <span class="counter-label">
          <!-- ko if: getCartParam('summary_count') == 1 -->
          <!-- ko text: getCartParam('summary_count') --><!-- /ko -->
          <!-- ko i18n: 'item' --><!-- /ko -->
          <span data-bind="html: getCartParam('subtotal')"></span>
          <!-- /ko -->
          <!-- ko if: getCartParam('summary_count') != 1 -->
          <!-- ko text: getCartParam('summary_count') --><!-- /ko -->
          <!-- ko i18n: 'items' --><!-- /ko -->
          <span data-bind="html: getCartParam('subtotal')"></span>
          <!-- /ko -->
          </span>


          It seems like you can also use the standard knockout.js data binding and don't persé need to use the crazy magento 2 knockout comment method. this solved the problem where rendering the getCartParam('subtotal') with the html method where it would normally print the subtotal incorrectly due to the <span ="price"></span> tag






          share|improve this answer



























            7












            7








            7







            We had a similar question from a client. where he wanted to show "[quantity] item [subtotal]" in a styled cart block instead of the default cart icon in the minicart.



            We found this question here but didn't like the answer where we needed to extend the MagentoCheckoutCustomerDataCart class just to render some html correctly



            this is the code how we fixed it in the template:



            <span class="counter-label">
            <!-- ko if: getCartParam('summary_count') == 1 -->
            <!-- ko text: getCartParam('summary_count') --><!-- /ko -->
            <!-- ko i18n: 'item' --><!-- /ko -->
            <span data-bind="html: getCartParam('subtotal')"></span>
            <!-- /ko -->
            <!-- ko if: getCartParam('summary_count') != 1 -->
            <!-- ko text: getCartParam('summary_count') --><!-- /ko -->
            <!-- ko i18n: 'items' --><!-- /ko -->
            <span data-bind="html: getCartParam('subtotal')"></span>
            <!-- /ko -->
            </span>


            It seems like you can also use the standard knockout.js data binding and don't persé need to use the crazy magento 2 knockout comment method. this solved the problem where rendering the getCartParam('subtotal') with the html method where it would normally print the subtotal incorrectly due to the <span ="price"></span> tag






            share|improve this answer















            We had a similar question from a client. where he wanted to show "[quantity] item [subtotal]" in a styled cart block instead of the default cart icon in the minicart.



            We found this question here but didn't like the answer where we needed to extend the MagentoCheckoutCustomerDataCart class just to render some html correctly



            this is the code how we fixed it in the template:



            <span class="counter-label">
            <!-- ko if: getCartParam('summary_count') == 1 -->
            <!-- ko text: getCartParam('summary_count') --><!-- /ko -->
            <!-- ko i18n: 'item' --><!-- /ko -->
            <span data-bind="html: getCartParam('subtotal')"></span>
            <!-- /ko -->
            <!-- ko if: getCartParam('summary_count') != 1 -->
            <!-- ko text: getCartParam('summary_count') --><!-- /ko -->
            <!-- ko i18n: 'items' --><!-- /ko -->
            <span data-bind="html: getCartParam('subtotal')"></span>
            <!-- /ko -->
            </span>


            It seems like you can also use the standard knockout.js data binding and don't persé need to use the crazy magento 2 knockout comment method. this solved the problem where rendering the getCartParam('subtotal') with the html method where it would normally print the subtotal incorrectly due to the <span ="price"></span> tag







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Oct 18 '16 at 11:33









            7ochem

            5,80493768




            5,80493768










            answered Oct 18 '16 at 11:09









            Pascal WientjesPascal Wientjes

            424411




            424411





















                4














                This above code will works on the page load, but will not work with magento2 ajax add to cart as it uses Knockout JS now.



                For that you should use -



                1. Override the magento class "MagentoCheckoutCustomerDataCart" in your module and extend method "getSectionData"



                public function getSectionData()

                $objectManager = MagentoFrameworkAppObjectManager::getInstance(); // Instance of Object Manager
                $priceHelper = $objectManager->create('MagentoFrameworkPricingHelperData'); // Instance of Pricing Helper

                $totals = $this->getQuote()->getTotals();
                return [
                'summary_count' => $this->getSummaryCount(),
                'subtotal' => isset($totals['subtotal'])
                ? $this->checkoutHelper->formatPrice($totals['subtotal']->getValue())
                : 0,
                'subtotal_value' => isset($totals['subtotal'])
                ? $priceHelper->currency($totals['subtotal']->getValue(),true,false)
                : '',
                'possible_onepage_checkout' => $this->isPossibleOnepageCheckout(),
                'items' => $this->getRecentItems(),
                'extra_actions' => $this->layout->createBlock('MagentoCatalogBlockShortcutButtons')->toHtml(),
                'isGuestCheckoutAllowed' => $this->isGuestCheckoutAllowed(),
                ];



                Here I have added a new cart param "subtotal_value" as the "subtotal" will return the price container span and it will display as TEXT using KO.
                Here you have to use "Object Manager Instance" directly, as you wont be able to inject dependencies to the "__construct".



                NOTE, there are few exception where we might need to use "Object Manager Instance" directly. In our case it is backward compatibility of constructor.

                ObjectManager Exception




                1. Next, copy magento default theme "/cart/minicart.phtml" to your theme and add the KO codes.



                  ko text: getCartParam('subtotal_value')







                share|improve this answer





























                  4














                  This above code will works on the page load, but will not work with magento2 ajax add to cart as it uses Knockout JS now.



                  For that you should use -



                  1. Override the magento class "MagentoCheckoutCustomerDataCart" in your module and extend method "getSectionData"



                  public function getSectionData()

                  $objectManager = MagentoFrameworkAppObjectManager::getInstance(); // Instance of Object Manager
                  $priceHelper = $objectManager->create('MagentoFrameworkPricingHelperData'); // Instance of Pricing Helper

                  $totals = $this->getQuote()->getTotals();
                  return [
                  'summary_count' => $this->getSummaryCount(),
                  'subtotal' => isset($totals['subtotal'])
                  ? $this->checkoutHelper->formatPrice($totals['subtotal']->getValue())
                  : 0,
                  'subtotal_value' => isset($totals['subtotal'])
                  ? $priceHelper->currency($totals['subtotal']->getValue(),true,false)
                  : '',
                  'possible_onepage_checkout' => $this->isPossibleOnepageCheckout(),
                  'items' => $this->getRecentItems(),
                  'extra_actions' => $this->layout->createBlock('MagentoCatalogBlockShortcutButtons')->toHtml(),
                  'isGuestCheckoutAllowed' => $this->isGuestCheckoutAllowed(),
                  ];



                  Here I have added a new cart param "subtotal_value" as the "subtotal" will return the price container span and it will display as TEXT using KO.
                  Here you have to use "Object Manager Instance" directly, as you wont be able to inject dependencies to the "__construct".



                  NOTE, there are few exception where we might need to use "Object Manager Instance" directly. In our case it is backward compatibility of constructor.

                  ObjectManager Exception




                  1. Next, copy magento default theme "/cart/minicart.phtml" to your theme and add the KO codes.



                    ko text: getCartParam('subtotal_value')







                  share|improve this answer



























                    4












                    4








                    4







                    This above code will works on the page load, but will not work with magento2 ajax add to cart as it uses Knockout JS now.



                    For that you should use -



                    1. Override the magento class "MagentoCheckoutCustomerDataCart" in your module and extend method "getSectionData"



                    public function getSectionData()

                    $objectManager = MagentoFrameworkAppObjectManager::getInstance(); // Instance of Object Manager
                    $priceHelper = $objectManager->create('MagentoFrameworkPricingHelperData'); // Instance of Pricing Helper

                    $totals = $this->getQuote()->getTotals();
                    return [
                    'summary_count' => $this->getSummaryCount(),
                    'subtotal' => isset($totals['subtotal'])
                    ? $this->checkoutHelper->formatPrice($totals['subtotal']->getValue())
                    : 0,
                    'subtotal_value' => isset($totals['subtotal'])
                    ? $priceHelper->currency($totals['subtotal']->getValue(),true,false)
                    : '',
                    'possible_onepage_checkout' => $this->isPossibleOnepageCheckout(),
                    'items' => $this->getRecentItems(),
                    'extra_actions' => $this->layout->createBlock('MagentoCatalogBlockShortcutButtons')->toHtml(),
                    'isGuestCheckoutAllowed' => $this->isGuestCheckoutAllowed(),
                    ];



                    Here I have added a new cart param "subtotal_value" as the "subtotal" will return the price container span and it will display as TEXT using KO.
                    Here you have to use "Object Manager Instance" directly, as you wont be able to inject dependencies to the "__construct".



                    NOTE, there are few exception where we might need to use "Object Manager Instance" directly. In our case it is backward compatibility of constructor.

                    ObjectManager Exception




                    1. Next, copy magento default theme "/cart/minicart.phtml" to your theme and add the KO codes.



                      ko text: getCartParam('subtotal_value')







                    share|improve this answer















                    This above code will works on the page load, but will not work with magento2 ajax add to cart as it uses Knockout JS now.



                    For that you should use -



                    1. Override the magento class "MagentoCheckoutCustomerDataCart" in your module and extend method "getSectionData"



                    public function getSectionData()

                    $objectManager = MagentoFrameworkAppObjectManager::getInstance(); // Instance of Object Manager
                    $priceHelper = $objectManager->create('MagentoFrameworkPricingHelperData'); // Instance of Pricing Helper

                    $totals = $this->getQuote()->getTotals();
                    return [
                    'summary_count' => $this->getSummaryCount(),
                    'subtotal' => isset($totals['subtotal'])
                    ? $this->checkoutHelper->formatPrice($totals['subtotal']->getValue())
                    : 0,
                    'subtotal_value' => isset($totals['subtotal'])
                    ? $priceHelper->currency($totals['subtotal']->getValue(),true,false)
                    : '',
                    'possible_onepage_checkout' => $this->isPossibleOnepageCheckout(),
                    'items' => $this->getRecentItems(),
                    'extra_actions' => $this->layout->createBlock('MagentoCatalogBlockShortcutButtons')->toHtml(),
                    'isGuestCheckoutAllowed' => $this->isGuestCheckoutAllowed(),
                    ];



                    Here I have added a new cart param "subtotal_value" as the "subtotal" will return the price container span and it will display as TEXT using KO.
                    Here you have to use "Object Manager Instance" directly, as you wont be able to inject dependencies to the "__construct".



                    NOTE, there are few exception where we might need to use "Object Manager Instance" directly. In our case it is backward compatibility of constructor.

                    ObjectManager Exception




                    1. Next, copy magento default theme "/cart/minicart.phtml" to your theme and add the KO codes.



                      ko text: getCartParam('subtotal_value')








                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited May 17 '18 at 7:54









                    Teja Bhagavan Kollepara

                    3,00641949




                    3,00641949










                    answered Sep 28 '16 at 9:36









                    Sandipan SSandipan S

                    47137




                    47137





















                        0














                        Define a block in your layout in which class marked as "MagentoCheckoutBlockCartTotals"



                        <block class="MagentoCheckoutBlockCartTotals" name="quote.print.totals" as="quote.print.totals" after="checkout.cart" 
                        template="MyNamespace_PrintCart::totals.phtml"/>


                        Then in .phtml you can have below code



                        <?php 
                        $totals = $block->getTotals() ;
                        ?>
                        <table class="data table totals">
                        <tbody>
                        <?php foreach($totals as $key => $total) :?>
                        <?php if(!empty($total->getValue())) :?>
                        <tr>
                        <td><?= $total->getTitle()->getText() ?></th>
                        <td>
                        <span class="price"><?= $this->helper('MagentoFrameworkPricingHelperData')->currency(number_format($total->getValue(),2),true,false) ?></span>
                        </td>
                        </tr>
                        <?php endif ?>
                        <?php endforeach ?>
                        </tbody>
                        </table>


                        Expected Output



                        enter image description here






                        share|improve this answer



























                          0














                          Define a block in your layout in which class marked as "MagentoCheckoutBlockCartTotals"



                          <block class="MagentoCheckoutBlockCartTotals" name="quote.print.totals" as="quote.print.totals" after="checkout.cart" 
                          template="MyNamespace_PrintCart::totals.phtml"/>


                          Then in .phtml you can have below code



                          <?php 
                          $totals = $block->getTotals() ;
                          ?>
                          <table class="data table totals">
                          <tbody>
                          <?php foreach($totals as $key => $total) :?>
                          <?php if(!empty($total->getValue())) :?>
                          <tr>
                          <td><?= $total->getTitle()->getText() ?></th>
                          <td>
                          <span class="price"><?= $this->helper('MagentoFrameworkPricingHelperData')->currency(number_format($total->getValue(),2),true,false) ?></span>
                          </td>
                          </tr>
                          <?php endif ?>
                          <?php endforeach ?>
                          </tbody>
                          </table>


                          Expected Output



                          enter image description here






                          share|improve this answer

























                            0












                            0








                            0







                            Define a block in your layout in which class marked as "MagentoCheckoutBlockCartTotals"



                            <block class="MagentoCheckoutBlockCartTotals" name="quote.print.totals" as="quote.print.totals" after="checkout.cart" 
                            template="MyNamespace_PrintCart::totals.phtml"/>


                            Then in .phtml you can have below code



                            <?php 
                            $totals = $block->getTotals() ;
                            ?>
                            <table class="data table totals">
                            <tbody>
                            <?php foreach($totals as $key => $total) :?>
                            <?php if(!empty($total->getValue())) :?>
                            <tr>
                            <td><?= $total->getTitle()->getText() ?></th>
                            <td>
                            <span class="price"><?= $this->helper('MagentoFrameworkPricingHelperData')->currency(number_format($total->getValue(),2),true,false) ?></span>
                            </td>
                            </tr>
                            <?php endif ?>
                            <?php endforeach ?>
                            </tbody>
                            </table>


                            Expected Output



                            enter image description here






                            share|improve this answer













                            Define a block in your layout in which class marked as "MagentoCheckoutBlockCartTotals"



                            <block class="MagentoCheckoutBlockCartTotals" name="quote.print.totals" as="quote.print.totals" after="checkout.cart" 
                            template="MyNamespace_PrintCart::totals.phtml"/>


                            Then in .phtml you can have below code



                            <?php 
                            $totals = $block->getTotals() ;
                            ?>
                            <table class="data table totals">
                            <tbody>
                            <?php foreach($totals as $key => $total) :?>
                            <?php if(!empty($total->getValue())) :?>
                            <tr>
                            <td><?= $total->getTitle()->getText() ?></th>
                            <td>
                            <span class="price"><?= $this->helper('MagentoFrameworkPricingHelperData')->currency(number_format($total->getValue(),2),true,false) ?></span>
                            </td>
                            </tr>
                            <?php endif ?>
                            <?php endforeach ?>
                            </tbody>
                            </table>


                            Expected Output



                            enter image description here







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered yesterday









                            Suman-PHP4USuman-PHP4U

                            1927




                            1927



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Magento Stack Exchange!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f121271%2fmagento-2-get-cart-quote-total-in-minicart-phtml%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Sum ergo cogito? 1 nng

                                419 nièngy_Soadمي 19bal1.5o_g

                                Queiggey Chernihivv 9NnOo i Zw X QqKk LpB