Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Display Product/Variant prices in proper currencies in API v2
Damian Legawiecbebb3b188252
diff --git a/api/app/helpers/spree/api/v2/display_money_helper.rb b/api/app/helpers/spree/api/v2/display_money_helper.rbnew file mode 100644index 0000000000..2eadab8112--- /dev/null+++ b/api/app/helpers/spree/api/v2/display_money_helper.rb@@ -0,0 +1,43 @@+module Spree+ module Api+ module V2+ module DisplayMoneyHelper+ extend ActiveSupport::Concern++ class_methods do+ def find_price(product_or_variant, currency)+ product_or_variant.price_in(currency)+ end++ def price(product_or_variant, currency)+ price = find_price(product_or_variant, currency)+ return nil if price.new_record?++ format('%.2f', price.amount)+ end++ def display_price(product_or_variant, currency)+ price = find_price(product_or_variant, currency)+ return nil if price.new_record?++ Spree::Money.new(price.amount, currency: currency).to_s+ end++ def compare_at_price(product_or_variant, currency)+ price = find_price(product_or_variant, currency)+ return nil if price.new_record? || price.compare_at_amount.blank?++ format('%.2f', price.compare_at_amount)+ end++ def display_compare_at_price(product_or_variant, currency)+ price = find_price(product_or_variant, currency)+ return nil if price.new_record? || price.compare_at_amount.blank?++ Spree::Money.new(price.compare_at_amount, currency: currency).to_s+ end+ end+ end+ end+ end+enddiff --git a/api/app/serializers/spree/v2/storefront/product_serializer.rb b/api/app/serializers/spree/v2/storefront/product_serializer.rbindex ee8bb17c8c..724b02fe60 100644--- a/api/app/serializers/spree/v2/storefront/product_serializer.rb+++ b/api/app/serializers/spree/v2/storefront/product_serializer.rb@@ -2,11 +2,11 @@ module Spreemodule V2module Storefrontclass ProductSerializer < BaseSerializer+ include ::Spree::Api::V2::DisplayMoneyHelper+set_type :product- attributes :name, :description, :price, :currency, :display_price,- :compare_at_price, :display_compare_at_price, :available_on,- :slug, :meta_description, :meta_keywords, :updated_at+ attributes :name, :description, :available_on, :slug, :meta_description, :meta_keywords, :updated_atattribute :purchasable do |product|product.purchasable?@@ -24,6 +24,26 @@ module Spreeproduct.available?end+ attribute :currency do |_product, params|+ params[:currency]+ end++ attribute :price do |product, params|+ price(product, params[:currency])+ end++ attribute :display_price do |product, params|+ display_price(product, params[:currency])+ end++ attribute :compare_at_price do |product, params|+ compare_at_price(product, params[:currency])+ end++ attribute :display_compare_at_price do |product, params|+ display_compare_at_price(product, params[:currency])+ end+has_many :variantshas_many :option_typeshas_many :product_propertiesdiff --git a/api/app/serializers/spree/v2/storefront/variant_serializer.rb b/api/app/serializers/spree/v2/storefront/variant_serializer.rbindex bb6265792d..7f555f0a44 100644--- a/api/app/serializers/spree/v2/storefront/variant_serializer.rb+++ b/api/app/serializers/spree/v2/storefront/variant_serializer.rb@@ -2,10 +2,11 @@ module Spreemodule V2module Storefrontclass VariantSerializer < BaseSerializer+ include ::Spree::Api::V2::DisplayMoneyHelper+set_type :variant- attributes :sku, :price, :currency, :display_price, :weight, :height,- :width, :depth, :is_master, :options_text+ attributes :sku, :weight, :height, :width, :depth, :is_master, :options_textattribute :purchasable do |variant|variant.purchasable?@@ -19,6 +20,26 @@ module Spreevariant.backorderable?end+ attribute :currency do |_product, params|+ params[:currency]+ end++ attribute :price do |product, params|+ price(product, params[:currency])+ end++ attribute :display_price do |product, params|+ display_price(product, params[:currency])+ end++ attribute :compare_at_price do |product, params|+ compare_at_price(product, params[:currency])+ end++ attribute :display_compare_at_price do |product, params|+ display_compare_at_price(product, params[:currency])+ end+belongs_to :producthas_many :imageshas_many :option_valuesdiff --git a/api/spec/requests/spree/api/v2/storefront/products_spec.rb b/api/spec/requests/spree/api/v2/storefront/products_spec.rbindex 0f42c0d526..b7bf64d4e3 100644--- a/api/spec/requests/spree/api/v2/storefront/products_spec.rb+++ b/api/spec/requests/spree/api/v2/storefront/products_spec.rb@@ -237,23 +237,51 @@ describe 'API V2 Storefront Products Spec', type: :request docontext 'fetch products by curency param' dolet!(:store) { create(:store, default: true, supported_currencies: 'USD,EUR,GBP', default_currency: 'USD' ) }- context 'with supported currency' do+ context 'with default currency' dobefore { get '/api/v2/storefront/products?currency=USD' }- it 'return products' do- expect(json_response['data']).not_to eq []+ it 'returns products' do+ expect(json_response['data']).not_to be_emptyexpect(json_response['data'][0]['attributes']['currency']).to eq 'USD'+ expect(json_response['data'].count).to eq Spree::Product.available.count+ end+ end++ context 'with supported currency' do+ let(:product) { products.first }+ let(:currency) { 'EUR' }++ before do+ product.master.prices.create(currency: currency, amount: 99.90, compare_at_amount: 129.90)+ get "/api/v2/storefront/products?currency=#{currency}&include=default_variant"+ end++ it 'returns products with prices in that currency' do+ expect(json_response['data']).not_to be_empty+ expect(json_response['data'].count).to eq(1)+ expect(json_response['data'][0]['id']).to eq(product.id.to_s)+ expect(json_response['data'][0]['attributes']['currency']).to eq currency+ expect(json_response['data'][0]['attributes']['price']).to eq('99.90')+ expect(json_response['data'][0]['attributes']['display_price']).to eq('€99.90')+ expect(json_response['data'][0]['attributes']['compare_at_price']).to eq('129.90')+ expect(json_response['data'][0]['attributes']['display_compare_at_price']).to eq('€129.90')+ expect(json_response['included'][0]['id']).to eq(product.default_variant_id.to_s)+ expect(json_response['included'][0]['type']).to eq('variant')+ expect(json_response['included'][0]['attributes']['price']).to eq('99.90')+ expect(json_response['included'][0]['attributes']['display_price']).to eq('€99.90')+ expect(json_response['included'][0]['attributes']['compare_at_price']).to eq('129.90')+ expect(json_response['included'][0]['attributes']['display_compare_at_price']).to eq('€129.90')endendcontext 'without supported currency' dobefore { get '/api/v2/storefront/products?currency=PLN' }- it 'return empty result' do- expect(json_response['data']).to eq []+ it 'returns empty result' do+ expect(json_response['data']).to be_empty+ expect(json_response['data'].count).to be_zeroendend-endend@@ -264,18 +292,44 @@ describe 'API V2 Storefront Products Spec', type: :request doit_behaves_like 'returns 200 HTTP status'it 'return product with supported currency' do- expect(json_response['data']).not_to be []- expect(json_response['data']['attributes']['slug']).to eq product.slug+ expect(json_response['data']).not_to be_empty+ expect(json_response['data']['id']).to eq(product.id.to_s)+ expect(json_response['data']['attributes']['currency']).to eq('USD')+ end+ end++ context 'with supported currency but without prices in that currency' do+ let!(:store) { create(:store, default: true, supported_currencies: 'USD,EUR,GBP', default_currency: 'USD') }++ before { get "/api/v2/storefront/products/#{product.slug}?currency=EUR&include=default_variant" }++ it_behaves_like 'returns 200 HTTP status'++ it 'returns empty prices' do+ expect(json_response['data']).not_to be_empty+ expect(json_response['data']['id']).to eq(product.id.to_s)+ expect(json_response['data']['attributes']['currency']).to eq('EUR')+ expect(json_response['data']['attributes']['price']).to be_nil+ expect(json_response['data']['attributes']['display_price']).to be_nil+ expect(json_response['data']['attributes']['compare_at_price']).to be_nil+ expect(json_response['data']['attributes']['display_compare_at_price']).to be_nil+ expect(json_response['included'][0]['id']).to eq(product.default_variant_id.to_s)+ expect(json_response['included'][0]['type']).to eq('variant')+ expect(json_response['included'][0]['attributes']['currency']).to eq('EUR')+ expect(json_response['included'][0]['attributes']['price']).to be_nil+ expect(json_response['included'][0]['attributes']['display_price']).to be_nil+ expect(json_response['included'][0]['attributes']['compare_at_price']).to be_nil+ expect(json_response['included'][0]['attributes']['display_compare_at_price']).to be_nilendendcontext 'without supported currency param' dobefore { get "/api/v2/storefront/products/#{product.slug}?currency=PLN" }- it 'return product with supported currency' do- expect(json_response['data']).not_to be []- expect(json_response['data']['attributes']['slug']).to eq product.slug- expect(json_response['data']['attributes']['currency']).to eq store.default_currency+ it 'fallbacks to default currency' do+ expect(json_response['data']).not_to be_empty+ expect(json_response['data']['id']).to eq(product.id.to_s)+ expect(json_response['data']['attributes']['currency']).to eq('USD')endend