Skip to content

Can only use hard-coded rates, not those from openexchangerates #23

@osuosu

Description

@osuosu

Really stuck on what to do here as I keep getting the error 'uncaught fx error' whenever I try to get the live rates (hard coding the rates works fine).

Here's the order I'm loading my scripts in:

<script src="jquery.js" type="text/javascript"></script>
<script src="accounting.min.js" type="text/javascript"></script>
<script src="money.min.js" type="text/javascript"></script>
<script src="myscript.js" type="text/javascript"></script>

And here's my jQuery code (myscript.js):

  /* ****************************************************************** */
    /* !CURRENCY CONVERTOR */
  /* ****************************************************************** */   

  // Set up money.js currencies - get latest rates with ajax:  
  $.getJSON(
    'https://openexchangerates.org/api/latest.json?app_id=[my_app_id]',
    function(data) {
      // Check money.js has finished loading:
      if ( typeof fx !== "undefined" && fx.rates ) {
        fx.rates = data.rates;
        fx.base = data.base;
      } else {
        // If not, apply to fxSetup global:
        var fxSetup = {
          rates : data.rates,
          base : data.base
        }
      }
    }
  );

  /*
  // Only hard-coding the rates like below works
  fx.base = "GBP";
  fx.rates = {
    "EUR" : 1.41, // eg. 1 GBP === 1.41 EUR
    "GBP" : 1, // always include the base rate (1:1)
    "SEK" : 13.18,
    "DKK" : 10.55,
    "NOK" : 13.21,
    "USD" : 1.52,
  }
  */

  // Functions to convert prices on collection and product pages
  function osuConvertCurr(priceContainerParent, priceContainer, currencyContainer, activeCurrency) {
    $(priceContainerParent).each(function() {

      var priceToConvert            = $(this).find(priceContainer).data('gbp-price');
      var priceConverted            = fx.convert(priceToConvert, {from: "GBP", to: activeCurrency});
          priceConverted            = accounting.toFixed(priceConverted, 0);

      $(this).find(priceContainer).text(priceConverted + ' ');
      $(this).find(currencyContainer).text(activeCurrency);

    });
  }
  function osuConvertCartItems(currency) {

    // Convert each item
    $('.actual-price').each(function() {

      var itemPrice                 = $(this).text();
      var itemPriceConverted        = fx.convert(itemPrice, {from: "GBP", to: currency});
          itemPriceConverted        = accounting.toFixed(itemPriceConverted, 0);

      $(this).text(itemPriceConverted + ' ');
      $(this).next().text(currency);

    });

  }
  function osuConvertCartCurr(currency) {

    // Convert subtotal
    var subtotal                    = $('.cart-subtotal--price').text();
    var subtotalConverted           = fx.convert(subtotal, {from: "GBP", to: currency});
        subtotalConverted           = accounting.toFixed(subtotalConverted, 0);

    $('.cart-subtotal').find('.cart-subtotal--price').text(subtotalConverted + ' ');
    $('.cart-subtotal').find('.product-currency').text(currency);

  }

  // Set correct dropdown state based on cookie value
  var cc_cookie = $.cookie('_cc_currency');
  if( cc_cookie == 'USD' ) {
    $('.currency-convertor option[value=USD]').prop("selected", "selected");
  } else if(cc_cookie == 'EUR') {
    $('.currency-convertor option[value=EUR]').prop("selected", "selected");
  } else if(cc_cookie == 'SEK') {
    $('.currency-convertor option[value=SEK]').prop("selected", "selected");
  } else if(cc_cookie == 'DKK') {
    $('.currency-convertor option[value=DKK]').prop("selected", "selected");
  } else if(cc_cookie == 'NOK') {
    $('.currency-convertor option[value=NOK]').prop("selected", "selected");
  } else {
    $('.currency-convertor option[value=GBP]').prop("selected", "selected");
  }

  // Set cookie value based on dropdown
  $('.currency-convertor select').on('change', function() {
    var optionValue = $(this).val();
    $.cookie('_cc_currency', optionValue, { expires: 14, path: '/' });
    location.reload();
  });


  // CONVERT CURRENCIES
  // --------------------------------------------------------------------
  if(cc_cookie !== 'GBP') {

    // Collections and single products
    if($('body').hasClass('template-collection') || $('body').hasClass('template-product')) {
      osuConvertCurr('.product-price', '.actual-price', '.product-currency', cc_cookie);
    }

    // Cart page
    if($('body').hasClass('template-cart')) {
      osuConvertCartItems(cc_cookie);
      osuConvertCartCurr(cc_cookie);
      $('.cart-subtotal p').slideDown('medium');
    }

  }

Any ideas of what's going on?

Thanks,

Osu

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions