I am using Knockout as a View Model to fill values of my view template, It is working fine for all except one function getConvertedTotal is returning NaN.

],function (ko, Component, urlBuilder,storage,quote) 
    'use strict';
    return Component.extend({
        defaults: {template: 'Ishaq_CheckoutTotal/custom-block-order-summary',},
        initialize: function(config) {
            var currencyRate = config.currencyRate;
            var currencySymbol = config.currencySymbol;
        getSubtotal: function() {
            var totals = quote.totals();
            return (totals ? totals : quote)['subtotal'];
        getGrandTotal: function() {
            var totals = quote.totals();
            return (totals ? totals : quote)['grand_total'] ;            
        getConvertedTotal: function() {
            var totals = quote.totals();
            var temp = (totals ? totals : quote)['grand_total'] ;
            return (temp*this.currencyRate);

This means one of your values are not a number, either temp or this.currencyRate. My guess is that this.currencyRate is not returning a number (probably undefined).

In that case you need to change:

var currencyRate = config.currencyRate


this.currencyRate = config.currencyRate

It's obvious from the code that this.currencyRate is undefined

  • Inside getConvertedTotal function unable to get value it is undefined, however inside initialize: function(config) I am able to get data using console.log
    – ishaq
    Feb 5 '21 at 5:07
  • 1
    I really hope you spend your time to learn some basic about javascript, this will save you alot of time when learning Magento or any other framework/library
    – Jimmy
    Feb 5 '21 at 7:37
  • +1 - if you're struggling with basic JS then Magento 2 is going to be extremely difficult to work on
    – Ben Crook
    Feb 5 '21 at 9:38
  • 1
    @ishaq That is because your currencySymbol variable is only available within the initialize function. You need to change it to this.currencyRate = config.currencyRate
    – Ben Crook
    Feb 5 '21 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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