Find a way to convert currencies with round rules and rates

Highlighted
New Member
3 0 1

I am making my little Shopify application and cannot understand how I can get the rounding rules for Shopify Plus store with multicurrency. There is Shopify.currency object in the JS console on the shop page, but rates, provided by this object don't take into the account rounding rules. How can I convert any product price to another currency?

 

Sample of my problem:

I have a 3.49 USD product in my cart: Shopify.currency #=> {rate: 1.0, active: 'USD'}

image

let's change the currency to CAD, for example {active: "CAD", rate: "1.35582685"}

Cart price will be 5.00 CAD, but

5.00 / 1.35582685 = 3.6877865340991

3.49 * 1.35582685 = 4.7318357065

Rates are very different from the amount in the cart.

image

 

1 Like
Highlighted
Shopify Staff
Shopify Staff
1555 81 282

Hey @lazynick.

 

At this time, we don't offer the specific conversion rate by any supported means. Unfortunately this means that you don't really have a way to get the exact conversion until we can offer a solution to this problem.

 

It's something we're seeing requested often, so it's definitely a pain point we're aware of.

0 Likes
Highlighted
Shopify Partner
2294 117 353

@Alex wrote:

It's something we're seeing requested often, so it's definitely a pain point we're aware of.


Add me to that request,  

Doing anything simplistic is further frustrated by the fact that:

  1. The exchange fee also isn't exposed anywhere, afaik but be nice if at minimum returned by the /cart/update endpoint or similar
  2. With a presentment currency the default currency price of products/variants is not available in liquid or on the json, afaik.
  3. On the javascript object Shopify.currency.rate the default currency exchange rate being  1.0 seems pointless except as a unit placeholder or js tricks.
  4. That shopify.currency js object property is ?undocumented? so untrustworthy,  should possibly be set on the cart.json object

 

Getting around this means hitting the shopify servers at least two times:

  1. once to set to the currency you want
  2. Fetching prices of all target products/variant
  3. in some case then resetting to the currency before #1

Then you can do your estimate+rounding.

 

Main story is these aspect not being exposed means the upgrade path of switching a theme over to using the new multicurrency and making it seemless is currently non-trivial.

When the merchant and customers are used to a smooth ajax switcher you pretty much have to rewrite new switcher code instead of being able to output multicurrency prices as hooks in the html and use what's already there.

 

 

 

Problem Solved? ✔️Accept and ? Like the solution so you can help others.
Buy me a coffee ☕ paypal.me/paulnewton or donate to eff.org
Confused? Busy? Buy a custom solution paull.newton+shopifyforum@gmail.com
1 Like
Highlighted
Shopify Partner
7 0 3

Hi,

 

You could create a visually hidden product (call it "Currency Base") and give it a price of $10,000,000 in your shop currency.

 

Knowing that you gave it a price of $10,000,000 (1000000000 in liquid terms) you can divide that by the converted price Shopify outputs your theme to get the conversion rate:

 

 {% assign baseRate = 1000000000 %}  
{% assign convertedRate = all_products['currency-base'].price | times: 1.0 %} {{ baseRate | divided_by: convertedRate }}

 

Hope this helps!

 

Sebastian

 

2 Likes
Highlighted
Shopify Partner
43 1 16

This is an excellent solution for getting Shopify’s currency rate in your liquid templates! I've used a similar method before for getting the currency rate via the API for backend functionality, but it never occurred to me that the same trick would work via liquid.

One slight change I would suggest though, is to set your currency product to 100 million dollars rather than 10 million. The reason is that Shopify's currency rate goes to 8 decimal places, and using a value less than this causes your new rate to be rounded by one decimal place. Practically speaking this won't make a difference unless you're selling absurdly high-cost items, but pedantically speaking it's nice to have the rate be exact . I've also confirmed that the rate you get by this method matches the Shopify.currency.rate Javascript object that is present on the Storefront.

Here is my updated version of your code to populate a variable with the currency rate:

 

{% assign currencyRate = all_products['currency-base'].price | divided_by: 10000000000.0 %}

 

And then to output the currency rate:

 

 {{ currencyRate }}

 

or to calculate a price:

 

{% assign customPrice = 1599 %}
{{ customPrice | times: currencyRate | money_with_currency }}

 


*Notes:

  • The decimal place in the currency calculation is essential, otherwise you'll end up with an integer rather than a decimal.
  • Remember that all prices in liquid are in cents, so 10 dollars would be 1000, 10.99 would be 1099.
  • In the calculation code, 'currency-base' needs to match with whatever the handle is of your currency product. This is called "URL and handle" under the SEO section at the bottom of the product editing page.
@Ginko wrote:

Hi,

You could create a visually hidden product (call it "Currency Base") and give it a price of $10,000,000 in your shop currency [...]

0 Likes