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.

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.rb
index 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 do
it 'returns products sorted by updated_at' do
expect(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)
end
end
@@ -214,7 +214,7 @@ describe 'API V2 Storefront Products Spec', type: :request do
it 'returns products sorted by updated_at with descending order' do
expect(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)
end
end
end
diff --git a/core/app/finders/spree/products/find.rb b/core/app/finders/spree/products/find.rb
index 8e3ea8d041..45a82e256b 100644
--- a/core/app/finders/spree/products/find.rb
+++ b/core/app/finders/spree/products/find.rb
@@ -105,19 +105,13 @@ module Spree
def 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)
end
def by_currency(products)
return products unless currency?
- products.joins(master: :prices).where(spree_prices: { currency: currency.upcase })
+ products.with_currency(currency)
end
def by_taxons(products)
@@ -225,7 +219,7 @@ module Spree
end
def include_discontinued(products)
- discontinued ? products : products.available
+ discontinued ? products : products.active(currency)
end
def taxon_ids(taxons_ids)
diff --git a/core/app/models/concerns/spree/product_scopes.rb b/core/app/models/concerns/spree/product_scopes.rb
index 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 Spree
add_simple_scopes simple_scopes
add_search_scope :ascend_by_master_price do
- joins(master: :default_price).order("#{price_table_name}.amount ASC")
+ order("#{price_table_name}.amount ASC")
end
add_search_scope :descend_by_master_price do
- joins(master: :default_price).order("#{price_table_name}.amount DESC")
+ order("#{price_table_name}.amount DESC")
end
add_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 })
end
add_search_scope :master_price_lte do |price|
- joins(master: :default_price).where("#{price_table_name}.amount <= ?", price)
+ where("#{price_table_name}.amount <= ?", price)
end
add_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 Spree
end
end
search_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
+
+ scope
end
search_scopes << :available
def self.active(currency = nil)
- available(nil, currency&.upcase)
+ available(nil, currency)
end
search_scopes << :active
diff --git a/core/lib/spree/core/search/base.rb b/core/lib/spree/core/search/base.rb
index e0c0f49785..ff5d7dc5c6 100644
--- a/core/lib/spree/core/search/base.rb
+++ b/core/lib/spree/core/search/base.rb
@@ -13,14 +13,7 @@ module Spree
end
def 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)
end
def method_missing(name)
@@ -34,7 +27,7 @@ module Spree
protected
def extended_base_scope
- base_scope = Spree::Product.spree_base_scopes.active
+ base_scope = Spree::Product.spree_base_scopes
base_scope = get_products_conditions_for(base_scope, keywords)
base_scope = Spree::Dependencies.products_finder.constantize.new(
scope: base_scope,
@@ -50,12 +43,11 @@ module Spree
current_currency: current_currency
).execute
base_scope = add_search_scopes(base_scope)
- base_scope = add_eagerload_scopes(base_scope)
- base_scope
+ add_eagerload_scopes(base_scope)
end
def 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 } }
]
)
- scope
end
def add_search_scopes(base_scope)
diff --git a/core/spec/models/spree/product/scopes_spec.rb b/core/spec/models/spree/product/scopes_spec.rb
index 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 do
it { 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) }
+ end
end
context 'A product assigned to parent and child taxons' do
diff --git a/frontend/spec/features/product_prices_spec.rb b/frontend/spec/features/product_prices_spec.rb
index 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: true
expect(page).to have_link product.name, href: "/products/#{product.slug}?currency=GBP"
open_i18n_menu
expect(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}"
end
end
end