Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
osuosu opened this issue Nov 15, 2015 · 0 comments
Open

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

osuosu opened this issue Nov 15, 2015 · 0 comments

Comments

@osuosu
Copy link

osuosu commented Nov 15, 2015

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant