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
Simplify currency handling for Product#available
Damian Legawiecd79f42e62044
diff --git a/api/spec/requests/spree/api/v2/storefront/products_spec.rb b/api/spec/requests/spree/api/v2/storefront/products_spec.rbindex 51e663e239..04a45cdde4 100644--- a/api/spec/requests/spree/api/v2/storefront/products_spec.rb+++ b/api/spec/requests/spree/api/v2/storefront/products_spec.rb@@ -203,7 +203,7 @@ describe 'API V2 Storefront Products Spec', type: :request doit 'returns products sorted by updated_at' doexpect(json_response['data'].count).to eq Spree::Product.available.count- expect(json_response['data'].pluck(:id)).to eq Spree::Product.available.order(:updated_at).pluck(:id).map(&:to_s)+ expect(json_response['data'].pluck(:id)).to eq Spree::Product.available.order(:updated_at).map(&:id).map(&:to_s)endend@@ -214,7 +214,7 @@ describe 'API V2 Storefront Products Spec', type: :request doit 'returns products sorted by updated_at with descending order' doexpect(json_response['data'].count).to eq Spree::Product.available.count- expect(json_response['data'].pluck(:id)).to eq Spree::Product.available.order(updated_at: :desc).pluck(:id).map(&:to_s)+ expect(json_response['data'].pluck(:id)).to eq Spree::Product.available.order(updated_at: :desc).map(&:id).map(&:to_s)endendenddiff --git a/core/app/finders/spree/products/find.rb b/core/app/finders/spree/products/find.rbindex 8e3ea8d041..45a82e256b 100644--- a/core/app/finders/spree/products/find.rb+++ b/core/app/finders/spree/products/find.rb@@ -105,19 +105,13 @@ module Spreedef by_price(products)return products unless price?- products.joins(master: :prices).- where(- spree_prices: {- amount: price.min..price.max,- currency: currency&.upcase- }- )+ products.price_between(price.min, price.max)enddef by_currency(products)return products unless currency?- products.joins(master: :prices).where(spree_prices: { currency: currency.upcase })+ products.with_currency(currency)enddef by_taxons(products)@@ -225,7 +219,7 @@ module Spreeenddef include_discontinued(products)- discontinued ? products : products.available+ discontinued ? products : products.active(currency)enddef taxon_ids(taxons_ids)diff --git a/core/app/models/concerns/spree/product_scopes.rb b/core/app/models/concerns/spree/product_scopes.rbindex f5d1bd01d3..f6ca24ef24 100644--- a/core/app/models/concerns/spree/product_scopes.rb+++ b/core/app/models/concerns/spree/product_scopes.rb@@ -43,23 +43,23 @@ module Spreeadd_simple_scopes simple_scopesadd_search_scope :ascend_by_master_price do- joins(master: :default_price).order("#{price_table_name}.amount ASC")+ order("#{price_table_name}.amount ASC")endadd_search_scope :descend_by_master_price do- joins(master: :default_price).order("#{price_table_name}.amount DESC")+ order("#{price_table_name}.amount DESC")endadd_search_scope :price_between do |low, high|- joins(master: :default_price).where(Price.table_name => { amount: low..high })+ where(Price.table_name => { amount: low..high })endadd_search_scope :master_price_lte do |price|- joins(master: :default_price).where("#{price_table_name}.amount <= ?", price)+ where("#{price_table_name}.amount <= ?", price)endadd_search_scope :master_price_gte do |price|- joins(master: :default_price).where("#{price_table_name}.amount >= ?", price)+ where("#{price_table_name}.amount >= ?", price)end# This scope selects products in taxon AND all its descendants@@ -198,15 +198,32 @@ module Spreeendendsearch_scopes << :not_discontinued++ def self.with_currency(currency)+ joins(variants_including_master: :prices).+ where(Price.table_name => { currency: currency.upcase }).+ where.not(Price.table_name => { amount: nil }).+ distinct+ end+ search_scopes << :with_currency+# Can't use add_search_scope for this as it needs a default argument- def self.available(available_on = nil, _currency = nil)+ def self.available(available_on = nil, currency = nil)available_on ||= Time.current- not_discontinued.joins(master: :prices).where("#{Product.quoted_table_name}.available_on <= ?", available_on)++ scope = not_discontinued.where("#{Product.quoted_table_name}.available_on <= ?", available_on)++ unless Spree::Config.show_products_without_price+ currency ||= Spree::Config[:currency]+ scope = scope.with_currency(currency)+ end++ scopeendsearch_scopes << :availabledef self.active(currency = nil)- available(nil, currency&.upcase)+ available(nil, currency)endsearch_scopes << :activediff --git a/core/lib/spree/core/search/base.rb b/core/lib/spree/core/search/base.rbindex e0c0f49785..ff5d7dc5c6 100644--- a/core/lib/spree/core/search/base.rb+++ b/core/lib/spree/core/search/base.rb@@ -13,14 +13,7 @@ module Spreeenddef retrieve_products- @products = extended_base_scope&.available- curr_page = page || 1-- unless Spree::Config.show_products_without_price- @products = @products.where('spree_prices.amount IS NOT NULL').- where('spree_prices.currency' => current_currency)- end- @products = @products.page(curr_page).per(per_page)+ @products = extended_base_scope.page(page || 1).per(per_page)enddef method_missing(name)@@ -34,7 +27,7 @@ module Spreeprotecteddef extended_base_scope- base_scope = Spree::Product.spree_base_scopes.active+ base_scope = Spree::Product.spree_base_scopesbase_scope = get_products_conditions_for(base_scope, keywords)base_scope = Spree::Dependencies.products_finder.constantize.new(scope: base_scope,@@ -50,12 +43,11 @@ module Spreecurrent_currency: current_currency).executebase_scope = add_search_scopes(base_scope)- base_scope = add_eagerload_scopes(base_scope)- base_scope+ add_eagerload_scopes(base_scope)enddef add_eagerload_scopes(scope)- scope = scope.includes(+ scope.includes(:tax_category,variants: [{ images: { attachment_attachment: :blob } }@@ -65,7 +57,6 @@ module Spree{ images: { attachment_attachment: :blob } }])- scopeenddef add_search_scopes(base_scope)diff --git a/core/spec/models/spree/product/scopes_spec.rb b/core/spec/models/spree/product/scopes_spec.rbindex ce7db3a331..9494925aa3 100644--- a/core/spec/models/spree/product/scopes_spec.rb+++ b/core/spec/models/spree/product/scopes_spec.rb@@ -27,6 +27,14 @@ describe 'Product scopes', type: :model doit { expect(Spree::Product.available).not_to include(unavailable_product) }end++ context 'different currency' do+ let!(:price_eur) { create(:price, variant: product.master, currency: 'EUR') }+ let!(:product_2) { create(:product) }++ it { expect(Spree::Product.available(nil, 'EUR')).to include(product) }+ it { expect(Spree::Product.available(nil, 'EUR')).not_to include(product_2) }+ endendcontext 'A product assigned to parent and child taxons' dodiff --git a/frontend/spec/features/product_prices_spec.rb b/frontend/spec/features/product_prices_spec.rbindex 814861b4c3..a3bf269aaf 100644--- a/frontend/spec/features/product_prices_spec.rb+++ b/frontend/spec/features/product_prices_spec.rb@@ -25,11 +25,6 @@ describe 'Product with prices in multiple currencies', type: :feature, js: trueexpect(page).to have_link product.name, href: "/products/#{product.slug}?currency=GBP"open_i18n_menuexpect(page).to have_select('switch_to_currency', selected: '£ GBP')- close_i18n_menu- switch_to_currency('USD')- expect(page).to have_text '$19.99'- expect(page).to have_current_path(spree.products_path)- expect(page).to have_link product.name, href: "/products/#{product.slug}"endendend