Question Solidus Gurus -- On our website we change...
# support
j
Question Solidus Gurus -- On our website we change the currency based on customer country, we used to do this via IP lookup and then set a cookie for the user, then we had basically overridden parts of solidus to pull pricing from the session cookie instead of the pricing selectors or the spree config. We just moved to cloudflare and see we can get the country code with every request for free, so we've change the system to remove all the cookies and instead set
Spree::Config.currency
on every request based on the cloudflare headers -- does this sound like a good solution or are we still handling this outside of the box?
b
I also get the data from Cloudflare, unfortunately traveling abroad or vpn's can make that unreliable.
You could store it somewhere on the browser or a table and let cloudflare prefill it if not already present.
j
Nice — do you set the currency through the config object as well?
Or not using it for currency?
b
I'm not actually that far on the MVP!
We store the currency and select based on the CloudFlare data. We also use it to select the region for their games.
But if I were to begin the currency feature right now, I would likely be passing it wherever Solidus has it standardized and then test the checkout from there.
Although I wouldn't set the config globally on each request. That seems like a bad idea.
There should be a way to check an order out and mark the currency of a line item/order.
j
Yeah I’m a little confused the right place to set it or if changing the the config is just for the single request or globally changing than and persisted
b
Multi currency orders may not be supported by solidus.
If it's the main speed config, that sounds global.
j
Yeah we don’t want that, the entire site should flip so all the pricing and everything changes on the product pages etc even if they don’t have an order yet
b
It would need a lambda setting to not be global.
j
I don’t really get how it could be though unless the config classes are making some sort of local cache on disk or something
b
My app is just going to explicitly state the currency. You checkout in a single currency. Setting the converted price makes me nervous though. Stripe should do the currency exchange for us at the moment they're charged.
j
We’ll it allows you to set the price in multiple currencies which works well
So we send the currency along with the total to stripe & paypal
But how to change the currency is not as clear 😂 before we had a validator which would set the currency on the current order to match the cookie but it still feels like this is in solidus somewhere in a way which isn’t clear to me still
They have the pricing options class but it’s a similar situation
b
Well, first up here's where the
Spree::Config.currency
gets used: https://github.com/solidusio/solidus/blob/main/core/config/initializers/money.rb#L7
So it's more a
default_currency
.
I'm already using the pricing options class. I use it to set the line item price equal to the stock item price if the selected stock item has a price associated to it: https://gist.github.com/BenMorganIO/9f457b54c8f224ecc089689e7cabc10f
j
Thanks so much for that snippet — I’m gonna take another crack at it, I didn’t see how I could switch the pricing option based on the headers with it but there must be a single place I can set the currency for the session 😅
b
j
Ohh nice that’s a really good idea as well
I just had to step out maybe when I crack this I’ll through a little blog post up
b
Added the migration.
❤️ 1
j
Thanks — happy to share some snippets from ours as well, it’s my first time using cloudflare and I gotta say it’s pretty badass what it gives Youn
You*
b
Yeah, and you don't need to send out a third party request for, well, each request.
j
Your pricing options class should be using the headers to figure out the currency. Changing it to use the built in extension point is probably pretty trivial, since you only need to change one of the factory methods on the class
I’d recommend using the built in logic. Your current approach is definitely unnecessarily “outside the box”, since the supported approach is basically just doing the same thing in a different place.
Also I recommend setting things like country in the session even if you’re inferring them from the CF headers and using that as source of truth so you can provide country switching down the road if need be.