Hi, can someone help me understand the product pri...
# support
a
Hi, can someone help me understand the product prices, I gave one of my products a price in GBP and I’d like to show UK users the price in GBP and others the price in EUR, is this possible within the same store ?
k
If I recall correctly, you need to create your own pricing option class (which is used here: https://github.com/solidusio/solidus/blob/master/core/lib/spree/core/controller_helpers/pricing.rb#L14). As you can see, that class, responds to a
from_context
method, which is responsible to select the currency to use when picking the price of the variants. This is the default implementation: https://github.com/solidusio/solidus/blob/553c26e2a647dc0d8807a4393777670f1db20277/core/app/models/spree/variant/pricing_options.rb#L55-L60. In your class, you can use whatever logic you want to understand where to user is located and map that location with a specific currency.
a
Ok thank you very much @kennyadsl I’ll have a look at this !
e
no need for custom pricing option as long as you define the currency:
Copy code
[5] pry(main)> Spree::Variant.last.price_for(Spree::Variant::PricingOptions.new(currency: 'USD'))
=> #<Spree::Money:0x0000000111cf1018 @money=#<Money fractional:1599 currency:USD>, @options={:sign_before_symbol=>true, :currency=>"USD"}>

[6] pry(main)> Spree::Variant.last.price_for(Spree::Variant::PricingOptions.new(currency: 'MXN'))
=> #<Spree::Money:0x0000000112711278 @money=#<Money fractional:14000 currency:MXN>, @options={:sign_before_symbol=>true, :currency=>"MXN"}>
[7] pry(main)>
I've modified/created my own PricingOption when I need an extra property, like a role or pricing level
k
but you need to change all views to do that (passing the current currency), isn’t it?
e
oh, right, you have to specify the currency in your views 👍
k
that’s why I was suggesting to customize the
from_context
method, in the context we already have that information
e
yes! I misread, I thought both prices at the same time but if he's going to render different prices for different users, he can also create multiple stores and modify Spree::Config.current_store_selector_class with a new class extending from Spree:StoreSelector:ByServerName, it'd set automatically the right store with the desired currency, and everything else should work out of the box, assuming the app will use the same domain
a
I don’t want to force the currency upon the user so maybe I should add a currency selector and let the user choose and then use that in my own pricing option class. Basically I want to serve both UK customers in GBP and EU customers in EUR from the same website (same domain). what would be the easiest solution to do so ? I’m wondering what other people do in this case I noticed the stores section in admin allowing multiple stores but that depends on the url and I want everything else to be the same (products, payment methods, etc ..)
k
I think the solution I proposed should work for what you described
a
Great ! Thanks for confirming, I’ll go with your solution then.