Skip to content

armgit / spree

Public Imported from GitHub
0

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.

Always return available products in the current currency in API v3

Damian Legawiec1135c473667a
diff --git a/api/app/controllers/spree/api/v3/store/products_controller.rb b/api/app/controllers/spree/api/v3/store/products_controller.rb
index a007bf72bf..d901cdca65 100644
--- a/api/app/controllers/spree/api/v3/store/products_controller.rb
+++ b/api/app/controllers/spree/api/v3/store/products_controller.rb
@@ -24,9 +24,10 @@ module Spree
end
def scope
- super.available
+ super.available(Time.current, Spree::Current.currency)
end
+ # these scopes are not automatically picked by ar_lazy_preload gem and we need to explicitly include them
def scope_includes
[
master: [:prices, { stock_items: :stock_location }],
diff --git a/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb b/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb
index 60dd47d963..59ac274ec1 100644
--- a/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb
+++ b/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb
@@ -71,6 +71,39 @@ RSpec.describe Spree::Api::V3::Store::ProductsController, type: :controller do
end
end
+ context 'currency scoping' do
+ let!(:eur_only_product) do
+ create(:product, stores: [store], status: 'active').tap do |p|
+ p.master.prices.delete_all
+ p.master.set_price('EUR', 20.0)
+ end
+ end
+
+ before do
+ allow(store).to receive(:supported_currencies_list).and_return([Money::Currency.find('USD'), Money::Currency.find('EUR')])
+ Spree::Config.show_products_without_price = false
+ end
+
+ it 'only returns products with prices in the current currency' do
+ request.headers['x-spree-currency'] = 'USD'
+ get :index
+
+ ids = json_response['data'].map { |p| p['id'] }
+ expect(ids).to include(product.prefix_id)
+ expect(ids).to include(product2.prefix_id)
+ expect(ids).not_to include(eur_only_product.prefix_id)
+ end
+
+ it 'returns EUR products when EUR currency is requested' do
+ request.headers['x-spree-currency'] = 'EUR'
+ get :index
+
+ ids = json_response['data'].map { |p| p['id'] }
+ expect(ids).to include(eur_only_product.prefix_id)
+ expect(ids).not_to include(product.prefix_id)
+ end
+ end
+
context 'ransack filtering' do
it 'filters products by name' do
get :index, params: { q: { name_cont: product.name } }