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.

Fixed: don't show Price List prices on Product/Variant edit form

Damian Legawiecd16128c1f41a
diff --git a/admin/app/controllers/spree/admin/products_controller.rb b/admin/app/controllers/spree/admin/products_controller.rb
index f3d0c1129c..e61d245aad 100644
--- a/admin/app/controllers/spree/admin/products_controller.rb
+++ b/admin/app/controllers/spree/admin/products_controller.rb
@@ -172,7 +172,7 @@ module Spree
end
@product_prices = {}
- @product.prices.includes(:variant).each do |price|
+ @product.prices.base_prices.includes(:variant).each do |price|
@product_prices[price.variant.human_name] ||= {}
@product_prices[price.variant.human_name][price.currency.downcase] = {
id: price.id.to_s,
diff --git a/admin/app/controllers/spree/admin/variants_controller.rb b/admin/app/controllers/spree/admin/variants_controller.rb
index f05790836e..8d317c05be 100644
--- a/admin/app/controllers/spree/admin/variants_controller.rb
+++ b/admin/app/controllers/spree/admin/variants_controller.rb
@@ -74,7 +74,7 @@ module Spree
def build_prices
current_store.supported_currencies_list.each do |currency|
- @variant.prices.build(currency: currency) unless @variant.prices.exists?(currency: currency.iso_code)
+ @variant.prices.build(currency: currency) unless @variant.prices.base_prices.exists?(currency: currency.iso_code)
end
end
diff --git a/admin/app/views/spree/admin/variants/form/_pricing.html.erb b/admin/app/views/spree/admin/variants/form/_pricing.html.erb
index 1bc941c5b5..7bdd4c7104 100644
--- a/admin/app/views/spree/admin/variants/form/_pricing.html.erb
+++ b/admin/app/views/spree/admin/variants/form/_pricing.html.erb
@@ -23,7 +23,8 @@
</tr>
</thead>
<tbody>
- <%= f.fields_for :prices do |price_form| %>
+ <% base_prices = f.object.prices.select { |p| p.price_list_id.nil? } %>
+ <%= f.fields_for :prices, base_prices do |price_form| %>
<%= price_form.hidden_field :id if price_form.object.persisted? %>
<%= price_form.hidden_field :currency %>
<tr>
@@ -31,13 +32,13 @@
<td>
<%= price_form.spree_text_field :amount, disabled: !can?(:manage, price_form.object),
value: price_form.object.amount.present? ? Spree::Money.new(price_form.object.amount, currency: price_form.object.currency, symbol: '').to_s : '',
- append: currency_symbol(price_form.object.currency),
+ append: currency_symbol(price_form.object.currency),
label: false %>
</td>
<td>
<%= price_form.spree_text_field :compare_at_amount, disabled: !can?(:manage, price_form.object),
value: price_form.object.compare_at_amount.present? ? Spree::Money.new(price_form.object.compare_at_amount, currency: price_form.object.currency, symbol: '').to_s : '',
- append: currency_symbol(price_form.object.currency),
+ append: currency_symbol(price_form.object.currency),
label: false %>
</td>
</tr>
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb
index 5cce5c9680..ff4bd88d34 100644
--- a/core/app/models/spree/variant.rb
+++ b/core/app/models/spree/variant.rb
@@ -210,6 +210,8 @@ module Spree
product_name_or_sku_cont(query)
end
+ # Returns the human name of the variant.
+ # @return [String] the human name of the variant
def human_name
@human_name ||= option_values.
joins(option_type: :product_option_types).
@@ -218,10 +220,14 @@ module Spree
pluck(:presentation).join('/')
end
+ # Returns true if the variant is available.
+ # @return [Boolean] true if the variant is available
def available?
!discontinued? && product.available?
end
+ # Returns true if the variant is in stock or backorderable.
+ # @return [Boolean] true if the variant is in stock or backorderable
def in_stock_or_backorderable?
self.class.in_stock_or_backorderable.exists?(id: id)
end
@@ -246,15 +252,20 @@ module Spree
end
end
+ # Returns the options text of the variant.
+ # @return [String] the options text of the variant
def options_text
@options_text ||= Spree::Variants::OptionsPresenter.new(self).to_sentence
end
- # Default to master name
+ # Returns the exchange name of the variant.
+ # @return [String] the exchange name of the variant
def exchange_name
is_master? ? name : options_text
end
+ # Returns the descriptive name of the variant.
+ # @return [String] the descriptive name of the variant
def descriptive_name
is_master? ? name + ' - Master' : name + ' - ' + options_text
end
@@ -308,6 +319,9 @@ module Spree
end
end
+ # Sets the option values for the variant
+ # @param options [Array<Hash>] the options to set
+ # @return [void]
def options=(options = {})
options.each do |option|
next if option[:name].blank? || option[:value].blank?
@@ -316,6 +330,11 @@ module Spree
end
end
+ # Sets the option value for the given option name.
+ # @param opt_name [String] the option name to set the option value for
+ # @param opt_value [String] the option value to set
+ # @param opt_type_position [Integer] the position of the option type
+ # @return [void]
def set_option_value(opt_name, opt_value, opt_type_position = nil)
# no option values on master
return if is_master
@@ -352,10 +371,16 @@ module Spree
save
end
+ # Returns the option value for the given option name.
+ # @param opt_name [String] the option name to get the option value for
+ # @return [Spree::OptionValue] the option value for the given option name
def find_option_value(opt_name)
option_values.includes(:option_type).detect { |o| o.option_type.name.parameterize == opt_name.parameterize }
end
+ # Returns the presentation of the option value for the given option type.
+ # @param option_type [Spree::OptionType] the option type to get the option value for
+ # @return [String] the presentation of the option value for the given option type
def option_value(option_type)
if option_type.is_a?(Spree::OptionType)
option_values.detect { |o| o.option_type_id == option_type.id }.try(:presentation)
@@ -364,13 +389,17 @@ module Spree
end
end
+ # Returns the base price (global price, not from a price list) for the given currency.
+ # Use price_for(context) when you need to resolve prices including price lists.
+ # @param currency [String] the currency to get the price for
+ # @return [Spree::Price] the base price for the given currency
def price_in(currency)
currency = currency&.upcase
price = if prices.loaded? && prices.any?
- prices.detect { |p| p.currency == currency }
+ prices.detect { |p| p.currency == currency && p.price_list_id.nil? }
else
- prices.find_by(currency: currency)
+ prices.base_prices.find_by(currency: currency)
end
if price.nil?
@@ -388,21 +417,35 @@ module Spree
)
end
+ # Returns the amount for the given currency.
+ # @param currency [String] the currency to get the amount for
+ # @return [BigDecimal] the amount for the given currency
def amount_in(currency)
price_in(currency).try(:amount)
end
+ # Returns the compare at amount for the given currency.
+ # @param currency [String] the currency to get the compare at amount for
+ # @return [BigDecimal] the compare at amount for the given currency
def compare_at_amount_in(currency)
price_in(currency).try(:compare_at_amount)
end
+ # Sets the base price (global price, not for a price list) for the given currency.
+ # @param currency [String] the currency to set the price for
+ # @param amount [BigDecimal] the amount to set
+ # @param compare_at_amount [BigDecimal] the compare at amount to set
+ # @return [void]
def set_price(currency, amount, compare_at_amount = nil)
- price = prices.find_or_initialize_by(currency: currency)
+ price = prices.base_prices.find_or_initialize_by(currency: currency)
price.amount = amount
price.compare_at_amount = compare_at_amount if compare_at_amount.present?
price.save!
end
+ # Returns the price for the given context or options.
+ # @param context_or_options [Spree::Pricing::Context|Hash] the context or options to get the price for
+ # @return [Spree::Price] the price for the given context or options
def price_for(context_or_options)
context = if context_or_options.is_a?(Spree::Pricing::Context)
context_or_options
@@ -415,6 +458,11 @@ module Spree
Spree::Pricing::Resolver.new(context).resolve
end
+ # Sets the stock for the variant
+ # @param count_on_hand [Integer] the count on hand
+ # @param backorderable [Boolean] the backorderable flag
+ # @param stock_location [Spree::StockLocation] the stock location to set the stock for
+ # @return [void]
def set_stock(count_on_hand, backorderable = nil, stock_location = nil)
stock_location ||= Spree::Store.current.default_stock_location
stock_item = stock_items.find_or_initialize_by(stock_location: stock_location)
@@ -436,6 +484,9 @@ module Spree
end.sum
end
+ # Returns the price modifier amount of the variant.
+ # @param options [Hash] the options to get the price modifier amount for
+ # @return [BigDecimal] the price modifier amount of the variant
def price_modifier_amount(options = {})
return 0 unless options.present?
@@ -449,18 +500,26 @@ module Spree
end.sum
end
+ # Returns the compare at price of the variant.
+ # @return [BigDecimal] the compare at price of the variant
def compare_at_price
@compare_at_price ||= price_in(cost_currency).try(:compare_at_amount)
end
+ # Returns the name and sku of the variant.
+ # @return [String] the name and sku of the variant
def name_and_sku
"#{name} - #{sku}"
end
+ # Returns the sku and options text of the variant.
+ # @return [String] the sku and options text of the variant
def sku_and_options_text
"#{sku} #{options_text}".strip
end
+ # Returns true if the variant is in stock.
+ # @return [Boolean] true if the variant is in stock
def in_stock?
@in_stock ||= if association(:stock_items).loaded? && association(:stock_locations).loaded?
total_on_hand.positive?
@@ -471,6 +530,8 @@ module Spree
end
end
+ # Returns true if the variant is backorderable.
+ # @return [Boolean] true if the variant is backorderable
def backorderable?
@backorderable ||= Rails.cache.fetch(['variant-backorderable', cache_key_with_version]) do
quantifier.backorderable?
diff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rb
index 2ac6171162..af0efbc030 100644
--- a/core/spec/models/spree/variant_spec.rb
+++ b/core/spec/models/spree/variant_spec.rb
@@ -594,6 +594,69 @@ describe Spree::Variant, type: :model do
expect(subject.to_s).to eql '£0.00'
end
end
+
+ context 'when price exists in a price list' do
+ let(:currency) { 'GBP' }
+ let(:price_list) { create(:price_list) }
+
+ before do
+ create(:price, variant: variant, currency: 'GBP', amount: 50.00, price_list: price_list)
+ end
+
+ it 'does not return price list price' do
+ expect(subject.to_s).to eql '£0.00'
+ end
+
+ context 'when base price also exists' do
+ before do
+ create(:price, variant: variant, currency: 'GBP', amount: 25.00)
+ end
+
+ it 'returns the base price, not the price list price' do
+ expect(subject.to_s).to eql '£25.00'
+ end
+ end
+ end
+ end
+
+ describe '#set_price' do
+ let(:currency) { 'GBP' }
+
+ it 'creates a base price for the currency' do
+ variant.set_price(currency, 25.00)
+
+ price = variant.prices.find_by(currency: currency)
+ expect(price.amount).to eq(25.00)
+ expect(price.price_list_id).to be_nil
+ end
+
+ it 'updates existing base price' do
+ create(:price, variant: variant, currency: currency, amount: 10.00)
+
+ variant.set_price(currency, 30.00)
+
+ expect(variant.prices.where(currency: currency).count).to eq(1)
+ expect(variant.prices.find_by(currency: currency).amount).to eq(30.00)
+ end
+
+ it 'does not update price list price' do
+ price_list = create(:price_list)
+ price_list_price = create(:price, variant: variant, currency: currency, amount: 50.00, price_list: price_list)
+
+ variant.set_price(currency, 25.00)
+
+ expect(variant.prices.where(currency: currency).count).to eq(2)
+ expect(price_list_price.reload.amount).to eq(50.00)
+ expect(variant.prices.base_prices.find_by(currency: currency).amount).to eq(25.00)
+ end
+
+ it 'sets compare_at_amount when provided' do
+ variant.set_price(currency, 25.00, 35.00)
+
+ price = variant.prices.find_by(currency: currency)
+ expect(price.amount).to eq(25.00)
+ expect(price.compare_at_amount).to eq(35.00)
+ end
end
describe '#on_sale?' do