0

I want to access the cart total from js. I have minicart-mixin.js in my custom module. I want to get the cart total there.

My file is minicart-mixin.js below.

define([
'uiComponent',
'Magento_Customer/js/customer-data',
'jquery',
'ko',
'underscore',
'sidebar',
'mage/translate',
'mage/dropdown'
], function (Component, customerData, $, ko, _) {
'use strict';

var mixin = {
    isButtonEnable: function () {
        /*You can add your condition here based on your requirements.*/
        console.log(customerData.get('cart')._latestValue.subtotal);
    }
};

return function (target) {
    return target.extend(mixin);
};
});

Console i am getting like this

<span class="price">$70.00</span>

How can i get the $70 only.

1
  • do you want cart subtotal, right? Commented Feb 5, 2020 at 14:03

1 Answer 1

4

You need to try the following solutions.

define([
    'uiComponent',
    'Magento_Checkout/js/model/totals'
], function (Component,totals) {
    'use strict';

    return Component.extend({
        getSubTotal: function(){
            if (totals.totals()) {
                var subtotal = parseFloat(totals.totals()['subtotal']);
                return subtotal;
            }
        },

    });
});
3
  • Thank you very much for your answer. and very sorry for late response. I got the value as customerData.get('cart')._latestValue.subtotalAmount.
    – Mujahidh
    Commented Feb 24, 2020 at 10:00
  • @Dhiren how can i put a condition if subtotal is greater than 100 display an element Commented Aug 13, 2020 at 13:28
  • @VinitKumar can you please raised separate question and give me link, so other community member also help you. Commented Aug 13, 2020 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.