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
Initial pass on Price Lists
Damian Legawiec861ef3af980e
diff --git a/admin/app/helpers/spree/admin/base_helper.rb b/admin/app/helpers/spree/admin/base_helper.rbindex 83cfc2715b..9bb5ad43b7 100644--- a/admin/app/helpers/spree/admin/base_helper.rb+++ b/admin/app/helpers/spree/admin/base_helper.rb@@ -135,14 +135,16 @@ module Spreerender 'spree/admin/preferences/password_field', form: form, field: field, options: optionswhen :textform.text_area(field, preference_field_options(options))+ when :datetime+ form.datetime_field(field, preference_field_options(options))elseform.text_field(field, preference_field_options(options))endend- # returns the options for a preference field, according to the type of the preference (number, decimal, boolean, string, password, text)+ # returns the options for a preference field, according to the type of the preference (number, decimal, boolean, string, password, text, datetime)# @param options [Hash] the options for the field- # @option options [Symbol] :type the type of the preference, eg. :integer, :decimal, :boolean, :string, :password, :text+ # @option options [Symbol] :type the type of the preference, eg. :integer, :decimal, :boolean, :string, :password, :text, :datetime# @option options [Boolean] :disabled whether the field is disabled# @return [Hash] the options for the fielddef preference_field_options(options)@@ -178,6 +180,10 @@ module Spreecols: 85,class: 'form-input'}+ when :datetime+ {+ class: 'form-input'+ }else{size: 10,diff --git a/api/app/helpers/spree/api/v2/display_money_helper.rb b/api/app/helpers/spree/api/v2/display_money_helper.rbindex 7be40fa6c0..497da6b756 100644--- a/api/app/helpers/spree/api/v2/display_money_helper.rb+++ b/api/app/helpers/spree/api/v2/display_money_helper.rb@@ -5,37 +5,57 @@ module Spreeextend ActiveSupport::Concernclass_methods do- def find_price(product_or_variant, currency)- product_or_variant.price_in(currency)+ def find_price(product_or_variant, currency, context_options = {})+ variant = product_or_variant.is_a?(Spree::Product) ? product_or_variant.default_variant : product_or_variant++ if context_options.present?+ context = build_pricing_context(variant, currency, context_options)+ variant.price_for(context)+ else+ variant.price_in(currency)+ endend- def price(product_or_variant, currency)- price = find_price(product_or_variant, currency)+ def price(product_or_variant, currency, context_options = {})+ price = find_price(product_or_variant, currency, context_options)return nil if price.new_record?price.amountend- def display_price(product_or_variant, currency)- price = find_price(product_or_variant, currency)+ def display_price(product_or_variant, currency, context_options = {})+ price = find_price(product_or_variant, currency, context_options)return nil if price.new_record?Spree::Money.new(price.amount, currency: currency).to_send- def compare_at_price(product_or_variant, currency)- price = find_price(product_or_variant, currency)+ def compare_at_price(product_or_variant, currency, context_options = {})+ price = find_price(product_or_variant, currency, context_options)return nil if price.new_record? || price.compare_at_amount.blank?price.compare_at_amountend- def display_compare_at_price(product_or_variant, currency)- price = find_price(product_or_variant, currency)+ def display_compare_at_price(product_or_variant, currency, context_options = {})+ price = find_price(product_or_variant, currency, context_options)return nil if price.new_record? || price.compare_at_amount.blank?Spree::Money.new(price.compare_at_amount, currency: currency).to_send++ private++ def build_pricing_context(variant, currency, options)+ Spree::Pricing::Context.new(+ variant: variant,+ currency: currency,+ store: options[:store],+ zone: options[:tax_zone] || options[:zone],+ user: options[:user],+ quantity: options[:quantity]+ )+ endendendenddiff --git a/api/app/serializers/spree/api/v2/platform/product_serializer.rb b/api/app/serializers/spree/api/v2/platform/product_serializer.rbindex a91958dbb1..f851b1e05f 100644--- a/api/app/serializers/spree/api/v2/platform/product_serializer.rb+++ b/api/app/serializers/spree/api/v2/platform/product_serializer.rb@@ -27,19 +27,27 @@ module Spreeendattribute :price do |product, params|- price(product, params[:currency])+ price(product, params[:currency], pricing_context_options(params))endattribute :display_price do |product, params|- display_price(product, params[:currency])+ display_price(product, params[:currency], pricing_context_options(params))endattribute :compare_at_price do |product, params|- compare_at_price(product, params[:currency])+ compare_at_price(product, params[:currency], pricing_context_options(params))endattribute :display_compare_at_price do |product, params|- display_compare_at_price(product, params[:currency])+ display_compare_at_price(product, params[:currency], pricing_context_options(params))+ end++ def self.pricing_context_options(params)+ {+ store: params[:store],+ user: params[:user],+ tax_zone: params.dig(:price_options, :tax_zone)+ }endbelongs_to :tax_category, serializer: Spree.api.platform_tax_category_serializerdiff --git a/api/app/serializers/spree/api/v2/platform/variant_serializer.rb b/api/app/serializers/spree/api/v2/platform/variant_serializer.rbindex 9de68b6129..0ea9e7cc94 100644--- a/api/app/serializers/spree/api/v2/platform/variant_serializer.rb+++ b/api/app/serializers/spree/api/v2/platform/variant_serializer.rb@@ -29,19 +29,27 @@ module Spreeendattribute :price do |object, params|- price(object, params[:currency])+ price(object, params[:currency], pricing_context_options(params))endattribute :display_price do |object, params|- display_price(object, params[:currency])+ display_price(object, params[:currency], pricing_context_options(params))endattribute :compare_at_price do |object, params|- compare_at_price(object, params[:currency])+ compare_at_price(object, params[:currency], pricing_context_options(params))endattribute :display_compare_at_price do |object, params|- display_compare_at_price(object, params[:currency])+ display_compare_at_price(object, params[:currency], pricing_context_options(params))+ end++ def self.pricing_context_options(params)+ {+ store: params[:store],+ user: params[:user],+ tax_zone: params.dig(:price_options, :tax_zone)+ }endbelongs_to :product, serializer: Spree.api.platform_product_serializerdiff --git a/api/app/serializers/spree/v2/storefront/product_serializer.rb b/api/app/serializers/spree/v2/storefront/product_serializer.rbindex 1f4e9042f4..3c0ab3f625 100644--- a/api/app/serializers/spree/v2/storefront/product_serializer.rb+++ b/api/app/serializers/spree/v2/storefront/product_serializer.rb@@ -30,19 +30,27 @@ module Spreeendattribute :price do |product, params|- price(product, params[:currency])+ price(product, params[:currency], pricing_context_options(params))endattribute :display_price do |product, params|- display_price(product, params[:currency])+ display_price(product, params[:currency], pricing_context_options(params))endattribute :compare_at_price do |product, params|- compare_at_price(product, params[:currency])+ compare_at_price(product, params[:currency], pricing_context_options(params))endattribute :display_compare_at_price do |product, params|- display_compare_at_price(product, params[:currency])+ display_compare_at_price(product, params[:currency], pricing_context_options(params))+ end++ def self.pricing_context_options(params)+ {+ store: params[:store],+ user: params[:user],+ tax_zone: params.dig(:price_options, :tax_zone)+ }endattribute :localized_slugs do |product, params|diff --git a/api/app/serializers/spree/v2/storefront/variant_serializer.rb b/api/app/serializers/spree/v2/storefront/variant_serializer.rbindex 50b35d68ac..a7115437ba 100644--- a/api/app/serializers/spree/v2/storefront/variant_serializer.rb+++ b/api/app/serializers/spree/v2/storefront/variant_serializer.rb@@ -26,19 +26,27 @@ module Spreeendattribute :price do |product, params|- price(product, params[:currency])+ price(product, params[:currency], pricing_context_options(params))endattribute :display_price do |product, params|- display_price(product, params[:currency])+ display_price(product, params[:currency], pricing_context_options(params))endattribute :compare_at_price do |product, params|- compare_at_price(product, params[:currency])+ compare_at_price(product, params[:currency], pricing_context_options(params))endattribute :display_compare_at_price do |product, params|- display_compare_at_price(product, params[:currency])+ display_compare_at_price(product, params[:currency], pricing_context_options(params))+ end++ def self.pricing_context_options(params)+ {+ store: params[:store],+ user: params[:user],+ tax_zone: params.dig(:price_options, :tax_zone)+ }endbelongs_to :product, serializer: Spree.api.storefront_product_serializerdiff --git a/api/spec/serializers/spree/v2/storefront/product_serializer_spec.rb b/api/spec/serializers/spree/v2/storefront/product_serializer_spec.rbindex ae45a8ca16..b25da88082 100644--- a/api/spec/serializers/spree/v2/storefront/product_serializer_spec.rb+++ b/api/spec/serializers/spree/v2/storefront/product_serializer_spec.rb@@ -5,7 +5,7 @@ describe Spree::V2::Storefront::ProductSerializer doinclude_context 'API v2 serializers params'- let(:product) { create(:product) }+ let(:product) { create(:product, price: 20) }context 'with tags' dobefore { product.tag_list.add('tag1', 'tag2') }@@ -22,4 +22,181 @@ describe Spree::V2::Storefront::ProductSerializer doexpect(subject[:data][:attributes][:labels]).to eq ['label1', 'label2']endend++ describe 'pricing attributes' do+ it 'returns base price attributes' do+ expect(subject[:data][:attributes]).to include(+ currency: currency,+ price: BigDecimal(20),+ display_price: '$20.00'+ )+ end+ end++ describe 'pricing context integration' do+ let(:other_store) { create(:store, default: false) }+ let(:other_zone) { create(:zone) }+ let(:user) { create(:user) }+ let(:taxon) { create(:taxon) }++ before do+ product.taxons << taxon+ end++ context 'with store-specific pricing' do+ let!(:price_list) { create(:price_list, status: 'active', priority: 100) }+ let!(:store_rule) { create(:store_price_rule, price_list: price_list, store_ids: [other_store.id]) }+ let!(:price_list_price) { create(:price, variant: product.master, currency: currency, amount: 12.50, price_list: price_list) }++ context 'when store matches' do+ let(:serializer_params) do+ {+ store: other_store,+ currency: currency,+ user: nil,+ locale: locale,+ price_options: { tax_zone: zone }+ }+ end++ it 'returns the store-specific price' do+ expect(subject[:data][:attributes][:price]).to eq(BigDecimal('12.50'))+ expect(subject[:data][:attributes][:display_price]).to eq('$12.50')+ end+ end++ context 'when store does not match' do+ it 'returns the base price' do+ expect(subject[:data][:attributes][:price]).to eq(BigDecimal(20))+ expect(subject[:data][:attributes][:display_price]).to eq('$20.00')+ end+ end+ end++ context 'with zone-specific pricing' do+ let!(:price_list) { create(:price_list, status: 'active', priority: 100) }+ let!(:zone_rule) { create(:zone_price_rule, price_list: price_list, zone_ids: [other_zone.id]) }+ let!(:price_list_price) { create(:price, variant: product.master, currency: currency, amount: 15.00, price_list: price_list) }++ context 'when zone matches' do+ let(:serializer_params) do+ {+ store: store,+ currency: currency,+ user: nil,+ locale: locale,+ price_options: { tax_zone: other_zone }+ }+ end++ it 'returns the zone-specific price' do+ expect(subject[:data][:attributes][:price]).to eq(BigDecimal('15.00'))+ expect(subject[:data][:attributes][:display_price]).to eq('$15.00')+ end+ end++ context 'when zone does not match' do+ it 'returns the base price' do+ expect(subject[:data][:attributes][:price]).to eq(BigDecimal(20))+ expect(subject[:data][:attributes][:display_price]).to eq('$20.00')+ end+ end+ end++ context 'with user-specific pricing' do+ let!(:price_list) { create(:price_list, status: 'active', priority: 100) }+ let!(:user_rule) { create(:user_price_rule, price_list: price_list, user_ids: [user.id]) }+ let!(:price_list_price) { create(:price, variant: product.master, currency: currency, amount: 14.00, price_list: price_list) }++ context 'when user matches' do+ let(:serializer_params) do+ {+ store: store,+ currency: currency,+ user: user,+ locale: locale,+ price_options: { tax_zone: zone }+ }+ end++ it 'returns the user-specific price' do+ expect(subject[:data][:attributes][:price]).to eq(BigDecimal('14.00'))+ expect(subject[:data][:attributes][:display_price]).to eq('$14.00')+ end+ end++ context 'when user does not match' do+ it 'returns the base price' do+ expect(subject[:data][:attributes][:price]).to eq(BigDecimal(20))+ expect(subject[:data][:attributes][:display_price]).to eq('$20.00')+ end+ end+ end++ context 'with taxon-specific pricing' do+ let!(:price_list) { create(:price_list, status: 'active', priority: 100) }+ let!(:taxon_rule) { create(:product_taxon_price_rule, price_list: price_list, taxon_ids: [taxon.id]) }+ let!(:price_list_price) { create(:price, variant: product.master, currency: currency, amount: 11.00, price_list: price_list) }++ it 'returns the taxon-specific price' do+ expect(subject[:data][:attributes][:price]).to eq(BigDecimal('11.00'))+ expect(subject[:data][:attributes][:display_price]).to eq('$11.00')+ end+ end++ context 'with combined rules (store AND zone)' do+ let!(:price_list) { create(:price_list, status: 'active', priority: 100, match_policy: 'all') }+ let!(:store_rule) { create(:store_price_rule, price_list: price_list, store_ids: [other_store.id]) }+ let!(:zone_rule) { create(:zone_price_rule, price_list: price_list, zone_ids: [other_zone.id]) }+ let!(:price_list_price) { create(:price, variant: product.master, currency: currency, amount: 9.00, price_list: price_list) }++ context 'when both store and zone match' do+ let(:serializer_params) do+ {+ store: other_store,+ currency: currency,+ user: nil,+ locale: locale,+ price_options: { tax_zone: other_zone }+ }+ end++ it 'returns the combined rule price' do+ expect(subject[:data][:attributes][:price]).to eq(BigDecimal('9.00'))+ expect(subject[:data][:attributes][:display_price]).to eq('$9.00')+ end+ end++ context 'when only store matches' do+ let(:serializer_params) do+ {+ store: other_store,+ currency: currency,+ user: nil,+ locale: locale,+ price_options: { tax_zone: zone }+ }+ end++ it 'returns the base price because both rules must match' do+ expect(subject[:data][:attributes][:price]).to eq(BigDecimal(20))+ expect(subject[:data][:attributes][:display_price]).to eq('$20.00')+ end+ end+ end++ context 'with multiple price lists' do+ let!(:low_priority_list) { create(:price_list, status: 'active', priority: 50) }+ let!(:high_priority_list) { create(:price_list, status: 'active', priority: 150) }+ let!(:store_rule_low) { create(:store_price_rule, price_list: low_priority_list, store_ids: [store.id]) }+ let!(:store_rule_high) { create(:store_price_rule, price_list: high_priority_list, store_ids: [store.id]) }+ let!(:low_price) { create(:price, variant: product.master, currency: currency, amount: 16.00, price_list: low_priority_list) }+ let!(:high_price) { create(:price, variant: product.master, currency: currency, amount: 10.00, price_list: high_priority_list) }++ it 'returns the highest priority price' do+ expect(subject[:data][:attributes][:price]).to eq(BigDecimal('10.00'))+ expect(subject[:data][:attributes][:display_price]).to eq('$10.00')+ end+ end+ endenddiff --git a/api/spec/serializers/spree/v2/storefront/variant_serializer_spec.rb b/api/spec/serializers/spree/v2/storefront/variant_serializer_spec.rbindex 5db3a8bf93..c198bfabbd 100644--- a/api/spec/serializers/spree/v2/storefront/variant_serializer_spec.rb+++ b/api/spec/serializers/spree/v2/storefront/variant_serializer_spec.rb@@ -41,4 +41,134 @@ describe Spree::V2::Storefront::VariantSerializer doit 'returns correct type' doexpect(subject[:data][:type]).to eq :variantend++ describe 'pricing context integration' do+ let(:other_store) { create(:store, default: false) }+ let(:other_zone) { create(:zone) }+ let(:user) { create(:user) }++ context 'with store-specific pricing' do+ let!(:price_list) { create(:price_list, status: 'active', priority: 100) }+ let!(:store_rule) { create(:store_price_rule, price_list: price_list, store_ids: [other_store.id]) }+ let!(:price_list_price) { create(:price, variant: variant, currency: currency, amount: 7.50, price_list: price_list) }++ context 'when store matches' do+ let(:serializer_params) do+ {+ store: other_store,+ currency: currency,+ user: nil,+ locale: locale,+ price_options: { tax_zone: zone }+ }+ end++ it 'returns the store-specific price' do+ expect(subject[:data][:attributes][:price]).to eq(BigDecimal('7.50'))+ expect(subject[:data][:attributes][:display_price]).to eq('$7.50')+ end+ end++ context 'when store does not match' do+ it 'returns the base price' do+ expect(subject[:data][:attributes][:price]).to eq(BigDecimal(10))+ expect(subject[:data][:attributes][:display_price]).to eq('$10.00')+ end+ end+ end++ context 'with zone-specific pricing' do+ let!(:price_list) { create(:price_list, status: 'active', priority: 100) }+ let!(:zone_rule) { create(:zone_price_rule, price_list: price_list, zone_ids: [other_zone.id]) }+ let!(:price_list_price) { create(:price, variant: variant, currency: currency, amount: 8.00, price_list: price_list) }++ context 'when zone matches' do+ let(:serializer_params) do+ {+ store: store,+ currency: currency,+ user: nil,+ locale: locale,+ price_options: { tax_zone: other_zone }+ }+ end++ it 'returns the zone-specific price' do+ expect(subject[:data][:attributes][:price]).to eq(BigDecimal('8.00'))+ expect(subject[:data][:attributes][:display_price]).to eq('$8.00')+ end+ end++ context 'when zone does not match' do+ it 'returns the base price' do+ expect(subject[:data][:attributes][:price]).to eq(BigDecimal(10))+ expect(subject[:data][:attributes][:display_price]).to eq('$10.00')+ end+ end+ end++ context 'with user-specific pricing' do+ let!(:price_list) { create(:price_list, status: 'active', priority: 100) }+ let!(:user_rule) { create(:user_price_rule, price_list: price_list, user_ids: [user.id]) }+ let!(:price_list_price) { create(:price, variant: variant, currency: currency, amount: 6.50, price_list: price_list) }++ context 'when user matches' do+ let(:serializer_params) do+ {+ store: store,+ currency: currency,+ user: user,+ locale: locale,+ price_options: { tax_zone: zone }+ }+ end++ it 'returns the user-specific price' do+ expect(subject[:data][:attributes][:price]).to eq(BigDecimal('6.50'))+ expect(subject[:data][:attributes][:display_price]).to eq('$6.50')+ end+ end++ context 'when user does not match' do+ it 'returns the base price' do+ expect(subject[:data][:attributes][:price]).to eq(BigDecimal(10))+ expect(subject[:data][:attributes][:display_price]).to eq('$10.00')+ end+ end+ end++ context 'with multiple price lists' do+ let!(:low_priority_list) { create(:price_list, status: 'active', priority: 50) }+ let!(:high_priority_list) { create(:price_list, status: 'active', priority: 100) }+ let!(:store_rule_low) { create(:store_price_rule, price_list: low_priority_list, store_ids: [store.id]) }+ let!(:store_rule_high) { create(:store_price_rule, price_list: high_priority_list, store_ids: [store.id]) }+ let!(:low_price) { create(:price, variant: variant, currency: currency, amount: 8.00, price_list: low_priority_list) }+ let!(:high_price) { create(:price, variant: variant, currency: currency, amount: 5.00, price_list: high_priority_list) }++ it 'returns the highest priority price' do+ expect(subject[:data][:attributes][:price]).to eq(BigDecimal('5.00'))+ expect(subject[:data][:attributes][:display_price]).to eq('$5.00')+ end+ end++ context 'with date-based pricing' do+ let!(:price_list) { create(:price_list, status: 'active', priority: 100, starts_at: 1.day.ago, ends_at: 1.day.from_now) }+ let!(:date_rule) { create(:date_range_price_rule, price_list: price_list, starts_at: 1.day.ago, ends_at: 1.day.from_now) }+ let!(:price_list_price) { create(:price, variant: variant, currency: currency, amount: 7.00, price_list: price_list) }++ it 'returns the date-specific price when within range' do+ expect(subject[:data][:attributes][:price]).to eq(BigDecimal('7.00'))+ expect(subject[:data][:attributes][:display_price]).to eq('$7.00')+ end++ context 'when outside date range' do+ let!(:price_list) { create(:price_list, status: 'active', priority: 100, starts_at: 2.days.from_now, ends_at: 3.days.from_now) }++ it 'returns the base price' do+ expect(subject[:data][:attributes][:price]).to eq(BigDecimal(10))+ expect(subject[:data][:attributes][:display_price]).to eq('$10.00')+ end+ end+ end+ endenddiff --git a/core/app/models/spree/line_item.rb b/core/app/models/spree/line_item.rbindex 06da051819..a596821262 100644--- a/core/app/models/spree/line_item.rb+++ b/core/app/models/spree/line_item.rb@@ -14,6 +14,7 @@ module Spreebelongs_to :variant, -> { with_deleted }, class_name: 'Spree::Variant'endbelongs_to :tax_category, -> { with_deleted }, class_name: 'Spree::TaxCategory'+ belongs_to :price_list, class_name: 'Spree::PriceList', optional: truehas_one :product, -> { with_deleted }, class_name: 'Spree::Product', through: :variant@@ -71,9 +72,11 @@ module Spreeenddef update_price- currency_price = variant.price_in(order.currency)+ context = Spree::Pricing::Context.from_order(variant, order, quantity: quantity)+ currency_price = variant.price_for(context)self.price = currency_price.price_including_vat_for(tax_zone: tax_zone) if currency_price.present?+ self.price_list_id = currency_price.price_list_id if currency_price.present?enddef copy_tax_categorydiff --git a/core/app/models/spree/price.rb b/core/app/models/spree/price.rbindex 6b522a4259..0a06dc2aba 100644--- a/core/app/models/spree/price.rb+++ b/core/app/models/spree/price.rb@@ -9,6 +9,7 @@ module SpreeMAXIMUM_AMOUNT = BigDecimal('99_999_999.99')belongs_to :variant, -> { with_deleted }, class_name: 'Spree::Variant', inverse_of: :prices, touch: true+ belongs_to :price_list, class_name: 'Spree::PriceList', optional: truebefore_validation :ensure_currencybefore_save :remove_compare_at_amount_if_equals_amount@@ -35,6 +36,8 @@ module Spreescope :with_currency, ->(currency) { where(currency: currency) }scope :non_zero, -> { where.not(amount: [nil, 0]) }scope :discounted, -> { where('compare_at_amount > amount') }+ scope :base_prices, -> { where(price_list_id: nil) }+ scope :for_price_list, ->(price_list) { where(price_list_id: price_list) }scope :for_products, ->(products, currency = nil) docurrency ||= Spree::Store.default.default_currencydiff --git a/core/app/models/spree/price_list.rb b/core/app/models/spree/price_list.rbnew file mode 100644index 0000000000..465e4d86cb--- /dev/null+++ b/core/app/models/spree/price_list.rb@@ -0,0 +1,85 @@+module Spree+ class PriceList < Spree::Base+ acts_as_paranoid++ STATUSES = %w[active inactive scheduled].freeze+ MATCH_POLICIES = %w[all any].freeze++ has_many :price_rules, class_name: 'Spree::PriceRule', dependent: :destroy+ has_many :prices, class_name: 'Spree::Price', dependent: :nullify++ validates :name, presence: true+ validates :priority, presence: true, numericality: { only_integer: true }+ validates :status, presence: true, inclusion: { in: STATUSES }+ validates :match_policy, presence: true, inclusion: { in: MATCH_POLICIES }+ validate :starts_at_before_ends_at++ scope :active, -> { where(status: 'active') }+ scope :inactive, -> { where(status: 'inactive') }+ scope :scheduled, -> { where(status: 'scheduled') }+ scope :by_priority, -> { order(priority: :desc) }+ scope :current, lambda {+ where('starts_at IS NULL OR starts_at <= ?', Time.current)+ .where('ends_at IS NULL OR ends_at >= ?', Time.current)+ }++ def self.for_context(context)+ active+ .current+ .by_priority+ end++ def applicable?(context)+ return false unless active_or_scheduled?+ return false unless within_date_range?(context.date || Time.current)++ rules_applicable?(context)+ end++ def rules_applicable?(context)+ return true if price_rules.none?++ case match_policy+ when 'all'+ price_rules.all? { |rule| rule.applicable?(context) }+ when 'any'+ price_rules.any? { |rule| rule.applicable?(context) }+ else+ false+ end+ end++ def active?+ status == 'active'+ end++ def inactive?+ status == 'inactive'+ end++ def scheduled?+ status == 'scheduled'+ end++ def active_or_scheduled?+ active? || scheduled?+ end++ private++ def starts_at_before_ends_at+ return if starts_at.blank? || ends_at.blank?++ if starts_at >= ends_at+ errors.add(:ends_at, :must_be_after_starts_at)+ end+ end++ def within_date_range?(date)+ return false if starts_at.present? && date < starts_at+ return false if ends_at.present? && date > ends_at++ true+ end+ end+enddiff --git a/core/app/models/spree/price_rule.rb b/core/app/models/spree/price_rule.rbnew file mode 100644index 0000000000..8dfa1103c3--- /dev/null+++ b/core/app/models/spree/price_rule.rb@@ -0,0 +1,25 @@+module Spree+ class PriceRule < Spree::Base+ acts_as_paranoid+ include Spree::Preferences::Preferable++ belongs_to :price_list, class_name: 'Spree::PriceList'++ validates :type, presence: true+ validates :priority, presence: true, numericality: { only_integer: true }++ scope :by_priority, -> { order(priority: :desc) }++ def applicable?(context)+ raise NotImplementedError, "#{self.class.name} must implement #applicable?"+ end++ def self.human_name+ name.demodulize.titleize+ end++ def self.description+ ''+ end+ end+enddiff --git a/core/app/models/spree/price_rules/date_range_rule.rb b/core/app/models/spree/price_rules/date_range_rule.rbnew file mode 100644index 0000000000..8b065b6075--- /dev/null+++ b/core/app/models/spree/price_rules/date_range_rule.rb@@ -0,0 +1,23 @@+module Spree+ module PriceRules+ class DateRangeRule < Spree::PriceRule+ preference :starts_at, :datetime+ preference :ends_at, :datetime++ def applicable?(context)+ date = context.date || Time.current+ timezone = context.store&.preferred_timezone || 'UTC'+ date = date.in_time_zone(timezone)++ return false if preferred_starts_at && date < preferred_starts_at+ return false if preferred_ends_at && date > preferred_ends_at++ true+ end++ def self.description+ 'Apply pricing within a specific date range'+ end+ end+ end+enddiff --git a/core/app/models/spree/price_rules/product_taxon_rule.rb b/core/app/models/spree/price_rules/product_taxon_rule.rbnew file mode 100644index 0000000000..ebd15893e9--- /dev/null+++ b/core/app/models/spree/price_rules/product_taxon_rule.rb@@ -0,0 +1,28 @@+module Spree+ module PriceRules+ class ProductTaxonRule < Spree::PriceRule+ preference :taxon_ids, :array, default: []+ preference :include_descendants, :boolean, default: true++ def applicable?(context)+ return true if preferred_taxon_ids.empty?++ product = context.variant.product+ product_taxon_ids = product.taxons.pluck(:id)++ if preferred_include_descendants+ taxon_ids_with_descendants = Spree::Taxon.where(id: preferred_taxon_ids)+ .map { |t| [t.id] + t.descendants.pluck(:id) }+ .flatten+ (product_taxon_ids & taxon_ids_with_descendants).any?+ else+ (product_taxon_ids & preferred_taxon_ids).any?+ end+ end++ def self.description+ 'Apply pricing based on product taxon'+ end+ end+ end+enddiff --git a/core/app/models/spree/price_rules/store_rule.rb b/core/app/models/spree/price_rules/store_rule.rbnew file mode 100644index 0000000000..7e2c306a4a--- /dev/null+++ b/core/app/models/spree/price_rules/store_rule.rb@@ -0,0 +1,18 @@+module Spree+ module PriceRules+ class StoreRule < Spree::PriceRule+ preference :store_ids, :array, default: []++ def applicable?(context)+ return false unless context.store+ return true if preferred_store_ids.empty?++ preferred_store_ids.include?(context.store.id)+ end++ def self.description+ 'Apply pricing based on which store the customer is shopping in'+ end+ end+ end+enddiff --git a/core/app/models/spree/price_rules/user_rule.rb b/core/app/models/spree/price_rules/user_rule.rbnew file mode 100644index 0000000000..59185a9fcb--- /dev/null+++ b/core/app/models/spree/price_rules/user_rule.rb@@ -0,0 +1,18 @@+module Spree+ module PriceRules+ class UserRule < Spree::PriceRule+ preference :user_ids, :array, default: []++ def applicable?(context)+ return false unless context.user+ return true if preferred_user_ids.empty?++ preferred_user_ids.include?(context.user.id)+ end++ def self.description+ 'Apply pricing to specific users'+ end+ end+ end+enddiff --git a/core/app/models/spree/price_rules/volume_rule.rb b/core/app/models/spree/price_rules/volume_rule.rbnew file mode 100644index 0000000000..ba66509d9a--- /dev/null+++ b/core/app/models/spree/price_rules/volume_rule.rb@@ -0,0 +1,38 @@+module Spree+ module PriceRules+ class VolumeRule < Spree::PriceRule+ preference :min_quantity, :integer, default: 1+ preference :max_quantity, :integer+ preference :apply_to, :string, default: 'line_item'++ APPLY_TO_OPTIONS = %w[line_item cart_total].freeze++ validates :preferred_apply_to, inclusion: { in: APPLY_TO_OPTIONS }, allow_nil: true++ def applicable?(context)+ quantity = calculate_quantity(context)+ return false unless quantity++ return false if quantity < preferred_min_quantity+ return false if preferred_max_quantity && quantity > preferred_max_quantity++ true+ end++ def self.description+ 'Apply pricing based on quantity purchased'+ end++ private++ def calculate_quantity(context)+ case preferred_apply_to+ when 'line_item'+ context.quantity+ when 'cart_total'+ context.order&.line_items&.find_by(variant: context.variant)&.quantity+ end+ end+ end+ end+enddiff --git a/core/app/models/spree/price_rules/zone_rule.rb b/core/app/models/spree/price_rules/zone_rule.rbnew file mode 100644index 0000000000..7004722445--- /dev/null+++ b/core/app/models/spree/price_rules/zone_rule.rb@@ -0,0 +1,18 @@+module Spree+ module PriceRules+ class ZoneRule < Spree::PriceRule+ preference :zone_ids, :array, default: []++ def applicable?(context)+ return false unless context.zone+ return true if preferred_zone_ids.empty?++ preferred_zone_ids.include?(context.zone.id)+ end++ def self.description+ 'Apply pricing based on the tax/shipping zone'+ end+ end+ end+enddiff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rbindex b0d076dbfb..5cce5c9680 100644--- a/core/app/models/spree/variant.rb+++ b/core/app/models/spree/variant.rb@@ -403,6 +403,18 @@ module Spreeprice.save!end+ def price_for(context_or_options)+ context = if context_or_options.is_a?(Spree::Pricing::Context)+ context_or_options+ elsif context_or_options.is_a?(Hash)+ Spree::Pricing::Context.new(**context_or_options.merge(variant: self))+ else+ raise ArgumentError, 'Must provide a Pricing::Context or options hash'+ end++ Spree::Pricing::Resolver.new(context).resolve+ end+def set_stock(count_on_hand, backorderable = nil, stock_location = nil)stock_location ||= Spree::Store.current.default_stock_locationstock_item = stock_items.find_or_initialize_by(stock_location: stock_location)diff --git a/core/config/locales/en.yml b/core/config/locales/en.ymlindex 0247aefb8d..076699d48f 100644--- a/core/config/locales/en.yml+++ b/core/config/locales/en.yml@@ -115,6 +115,17 @@ en:spree/policy:body: Bodyowner: Policy for+ spree/price_list:+ description: Description+ ends_at: Ends At+ match_policy: Match Policy+ name: Name+ priority: Priority+ starts_at: Starts At+ status: Status+ spree/price_rule:+ priority: Priority+ type: Typespree/product:available_on: Available Oncost_currency: Cost Currency@@ -281,6 +292,10 @@ en:attributes:amount:greater_than_max_amount: is greater than the allowed maximum amount of %{max_amount}+ spree/price_list:+ attributes:+ ends_at:+ must_be_after_starts_at: must be after starts atspree/product:attributes:base:@@ -413,6 +428,12 @@ en:spree/price:one: Priceother: Prices+ spree/price_list:+ one: Price List+ other: Price Lists+ spree/price_rule:+ one: Price Rule+ other: Price Rulesspree/product:one: Productother: Products@@ -1292,6 +1313,9 @@ en:match_choices:all: Allnone: None+ match_policies:+ all: Match ALL rules+ any: Match ANY rulemax: Maxmax_items: Max Itemsmedia: Media@@ -1658,7 +1682,30 @@ en:previous: Previouspreviously_used_card: Previously Used Cardsprice: Price+ price_list_statuses:+ active: Active+ inactive: Inactive+ scheduled: Scheduledprice_range: Price Range+ price_rule_types:+ date_range_rule:+ description: Apply pricing during specific dates+ name: Date Range+ product_taxon_rule:+ description: Apply pricing to products in specific categories+ name: Product Category+ store_rule:+ description: Apply different pricing per store+ name: Store+ user_rule:+ description: Apply pricing to specific customers+ name: User+ volume_rule:+ description: Apply pricing based on quantity+ name: Volume+ zone_rule:+ description: Apply pricing based on geographic zone+ name: Zoneprice_sack: Price Sackprices: Pricespricing: Pricingdiff --git a/core/db/migrate/20251110120000_create_spree_price_lists.rb b/core/db/migrate/20251110120000_create_spree_price_lists.rbnew file mode 100644index 0000000000..b8ae43a2a1--- /dev/null+++ b/core/db/migrate/20251110120000_create_spree_price_lists.rb@@ -0,0 +1,20 @@+class CreateSpreePriceLists < ActiveRecord::Migration[7.0]+ def change+ create_table :spree_price_lists do |t|+ t.string :name, null: false+ t.text :description+ t.integer :priority, null: false, default: 0+ t.string :status, null: false, default: 'active'+ t.datetime :starts_at+ t.datetime :ends_at+ t.string :match_policy, null: false, default: 'all'+ t.timestamps+ t.datetime :deleted_at+ end++ add_index :spree_price_lists, :status+ add_index :spree_price_lists, :priority+ add_index :spree_price_lists, [:starts_at, :ends_at]+ add_index :spree_price_lists, :deleted_at+ end+enddiff --git a/core/db/migrate/20251110120001_create_spree_price_rules.rb b/core/db/migrate/20251110120001_create_spree_price_rules.rbnew file mode 100644index 0000000000..7316dae3b7--- /dev/null+++ b/core/db/migrate/20251110120001_create_spree_price_rules.rb@@ -0,0 +1,17 @@+class CreateSpreePriceRules < ActiveRecord::Migration[7.0]+ def change+ create_table :spree_price_rules do |t|+ t.references :price_list, null: false+ t.string :type, null: false+ t.integer :priority, null: false, default: 0+ t.text :preferences+ t.timestamps+ t.datetime :deleted_at+ end++ add_index :spree_price_rules, [:price_list_id, :type]+ add_index :spree_price_rules, :type+ add_index :spree_price_rules, :priority+ add_index :spree_price_rules, :deleted_at+ end+enddiff --git a/core/db/migrate/20251110120002_add_price_list_id_to_spree_prices.rb b/core/db/migrate/20251110120002_add_price_list_id_to_spree_prices.rbnew file mode 100644index 0000000000..42ff77486d--- /dev/null+++ b/core/db/migrate/20251110120002_add_price_list_id_to_spree_prices.rb@@ -0,0 +1,6 @@+class AddPriceListIdToSpreePrices < ActiveRecord::Migration[7.0]+ def change+ add_reference :spree_prices, :price_list, null: true+ add_index :spree_prices, [:variant_id, :currency, :price_list_id], name: 'index_spree_prices_on_variant_currency_price_list'+ end+enddiff --git a/core/db/migrate/20251110120003_add_price_list_id_to_spree_line_items.rb b/core/db/migrate/20251110120003_add_price_list_id_to_spree_line_items.rbnew file mode 100644index 0000000000..26b257cc61--- /dev/null+++ b/core/db/migrate/20251110120003_add_price_list_id_to_spree_line_items.rb@@ -0,0 +1,5 @@+class AddPriceListIdToSpreeLineItems < ActiveRecord::Migration[7.0]+ def change+ add_reference :spree_line_items, :price_list, null: true+ end+enddiff --git a/core/lib/generators/spree/dummy/dummy_generator.rb b/core/lib/generators/spree/dummy/dummy_generator.rbindex c2f353ccb3..fd9d53bada 100644--- a/core/lib/generators/spree/dummy/dummy_generator.rb+++ b/core/lib/generators/spree/dummy/dummy_generator.rb@@ -89,7 +89,7 @@ module Spreedef inject_yaml_permitted_classesinside dummy_path doinject_into_file 'config/application.rb', %Q[- config.active_record.yaml_column_permitted_classes = [Symbol, BigDecimal, ActiveSupport::HashWithIndifferentAccess]+ config.active_record.yaml_column_permitted_classes = [Symbol, BigDecimal, ActiveSupport::HashWithIndifferentAccess, ActiveSupport::TimeWithZone]], after: /config\.load_defaults.*$/, verbose: trueendenddiff --git a/core/lib/spree/core.rb b/core/lib/spree/core.rbindex b0f14d07a6..2ffd73db31 100644--- a/core/lib/spree/core.rb+++ b/core/lib/spree/core.rb@@ -436,3 +436,5 @@ require 'spree/core/preferences/scoped_store'require 'spree/core/preferences/runtime_configuration'require 'spree/core/permission_configuration'+require 'spree/core/pricing/context'+require 'spree/core/pricing/resolver'diff --git a/core/lib/spree/core/engine.rb b/core/lib/spree/core/engine.rbindex cae1396a7e..1a3d3bca31 100644--- a/core/lib/spree/core/engine.rb+++ b/core/lib/spree/core/engine.rb@@ -12,6 +12,7 @@ module Spree:adjusters,:stock_splitters,:promotions,+ :pricing,:line_item_comparison_hooks,:data_feed_types,:export_types,@@ -31,6 +32,7 @@ module Spree:subscribers)SpreeCalculators = Struct.new(:shipping_methods, :tax_rates, :promotion_actions_create_adjustments, :promotion_actions_create_item_adjustments)PromoEnvironment = Struct.new(:rules, :actions)+ PricingEnvironment = Struct.new(:rules)SpreeValidators = Struct.new(:addresses)MetafieldsEnvironment = Struct.new(:types, :enabled_resources)isolate_namespace Spree@@ -47,7 +49,7 @@ module Spreeapp.config.spree = Environment.new(SpreeCalculators.new, SpreeValidators.new, Spree::Core::Configuration.new, Spree::Core::Dependencies.new)app.config.active_record.yaml_column_permitted_classes ||= []- app.config.active_record.yaml_column_permitted_classes.concat([Symbol, BigDecimal, ActiveSupport::HashWithIndifferentAccess])+ app.config.active_record.yaml_column_permitted_classes.concat([Symbol, BigDecimal, ActiveSupport::HashWithIndifferentAccess, ActiveSupport::TimeWithZone])Spree::Config = app.config.spree.preferencesSpree::RuntimeConfig = app.config.spree.preferences # for compatibilitySpree::Dependencies = app.config.spree.dependencies@@ -91,6 +93,12 @@ module Spreeinitializer 'spree.promo.register.promotion.calculators' do |app|end+ # Pricing configuration for price lists and price rules+ initializer 'spree.pricing.environment', after: 'spree.environment' do |app|+ app.config.spree.pricing = PricingEnvironment.new+ app.config.spree.pricing.rules = []+ end+# Promotion rules need to be evaluated on after initialize otherwise# Spree.user_class would be nil and users might experience errors related# to malformed model associations (Spree.user_class is only defined on@@ -155,6 +163,15 @@ module SpreeSpree::Promotion::Rules::OptionValue,]+ Rails.application.config.spree.pricing.rules.concat [+ 'Spree::PriceRules::StoreRule',+ 'Spree::PriceRules::ZoneRule',+ 'Spree::PriceRules::UserRule',+ 'Spree::PriceRules::DateRangeRule',+ 'Spree::PriceRules::VolumeRule',+ 'Spree::PriceRules::ProductTaxonRule'+ ]+Rails.application.config.spree.promotions.actions = [Promotion::Actions::CreateAdjustment,Promotion::Actions::CreateItemAdjustments,diff --git a/core/lib/spree/core/preferences/preferable.rb b/core/lib/spree/core/preferences/preferable.rbindex 3c1d19a90c..95a9e1c377 100644--- a/core/lib/spree/core/preferences/preferable.rb+++ b/core/lib/spree/core/preferences/preferable.rb@@ -162,6 +162,17 @@ module Spree::Preferences::Preferableelsevalue.class.ancestors.include?(Hash) ? value : {}end+ when :datetime+ return nil if value.blank?++ case value+ when Time, Date, DateTime, ActiveSupport::TimeWithZone+ value+ when String+ Time.zone.parse(value)+ else+ value.to_time+ endelsevalueenddiff --git a/core/lib/spree/core/pricing/context.rb b/core/lib/spree/core/pricing/context.rbnew file mode 100644index 0000000000..a88fe2644b--- /dev/null+++ b/core/lib/spree/core/pricing/context.rb@@ -0,0 +1,48 @@+module Spree+ module Pricing+ class Context+ attr_reader :variant, :currency, :store, :zone, :user, :quantity, :date, :order++ def initialize(variant:, currency:, store: nil, zone: nil, user: nil, quantity: nil, date: nil, order: nil)+ @variant = variant+ @currency = currency+ @store = store || Spree::Store.default+ @zone = zone+ @user = user+ @quantity = quantity+ @date = date || Time.current+ @order = order+ end++ def self.from_currency(variant, currency)+ new(variant: variant, currency: currency)+ end++ def self.from_order(variant, order, quantity: nil)+ new(+ variant: variant,+ currency: order.currency,+ store: order.store,+ zone: order.tax_zone || order.store.checkout_zone,+ user: order.user,+ quantity: quantity || order.line_items.find_by(variant: variant)&.quantity,+ order: order+ )+ end++ def cache_key+ [+ 'spree',+ 'pricing',+ variant.id,+ currency,+ store&.id,+ zone&.id,+ user&.id,+ quantity,+ date&.to_i+ ].compact.join('/')+ end+ end+ end+enddiff --git a/core/lib/spree/core/pricing/resolver.rb b/core/lib/spree/core/pricing/resolver.rbnew file mode 100644index 0000000000..fd269c2184--- /dev/null+++ b/core/lib/spree/core/pricing/resolver.rb@@ -0,0 +1,76 @@+module Spree+ module Pricing+ class Resolver+ attr_reader :context++ def initialize(context)+ @context = context+ end++ def resolve+ Rails.cache.fetch(cache_key, expires_in: 15.minutes) do+ find_best_price+ end+ end++ private++ def find_best_price+ # Try to find a price from applicable price lists first+ price_from_list = find_price_from_lists+ return price_from_list if price_from_list++ # Fall back to base price (no price_list_id)+ find_base_price+ end++ def find_price_from_lists+ applicable_price_lists.each do |price_list|+ price = find_price_for_list(price_list)+ return price if price+ end++ nil+ end++ def applicable_price_lists+ @applicable_price_lists ||= begin+ lists = Spree::PriceList.includes(:price_rules)+ .for_context(context)+ .to_a++ lists.select { |list| list.applicable?(context) }+ end+ end++ def find_price_for_list(price_list)+ context.variant.prices+ .with_currency(context.currency)+ .where(price_list_id: price_list.id)+ .non_zero+ .first+ end++ def find_base_price+ context.variant.prices+ .with_currency(context.currency)+ .where(price_list_id: nil)+ .where.not(amount: nil)+ .first || build_empty_price+ end++ def build_empty_price+ context.variant.prices.build(+ variant: context.variant,+ currency: context.currency,+ amount: nil,+ price_list_id: nil+ )+ end++ def cache_key+ "#{context.cache_key}/resolved_price"+ end+ end+ end+enddiff --git a/core/lib/spree/testing_support/factories/price_list_factory.rb b/core/lib/spree/testing_support/factories/price_list_factory.rbnew file mode 100644index 0000000000..1d4d0d84d8--- /dev/null+++ b/core/lib/spree/testing_support/factories/price_list_factory.rb@@ -0,0 +1,29 @@+FactoryBot.define do+ factory :price_list, class: Spree::PriceList do+ sequence(:name) { |n| "Price List #{n}" }+ priority { 0 }+ status { 'active' }+ match_policy { 'all' }++ trait :inactive do+ status { 'inactive' }+ end++ trait :scheduled do+ status { 'scheduled' }+ end++ trait :with_date_range do+ starts_at { 1.day.ago }+ ends_at { 1.day.from_now }+ end++ trait :high_priority do+ priority { 100 }+ end++ trait :any_match do+ match_policy { 'any' }+ end+ end+enddiff --git a/core/lib/spree/testing_support/factories/price_rule_factory.rb b/core/lib/spree/testing_support/factories/price_rule_factory.rbnew file mode 100644index 0000000000..54998a8629--- /dev/null+++ b/core/lib/spree/testing_support/factories/price_rule_factory.rb@@ -0,0 +1,74 @@+FactoryBot.define do+ factory :price_rule, class: Spree::PriceRule do+ price_list+ priority { 0 }++ factory :store_price_rule, class: Spree::PriceRules::StoreRule do+ after(:build) do |rule, evaluator|+ rule.preferred_store_ids = evaluator.store_ids if evaluator.respond_to?(:store_ids)+ end++ transient do+ store_ids { [] }+ end+ end++ factory :zone_price_rule, class: Spree::PriceRules::ZoneRule do+ after(:build) do |rule, evaluator|+ rule.preferred_zone_ids = evaluator.zone_ids if evaluator.respond_to?(:zone_ids)+ end++ transient do+ zone_ids { [] }+ end+ end++ factory :date_range_price_rule, class: Spree::PriceRules::DateRangeRule do+ after(:build) do |rule, evaluator|+ rule.preferred_starts_at = evaluator.starts_at if evaluator.starts_at.present?+ rule.preferred_ends_at = evaluator.ends_at if evaluator.ends_at.present?+ end++ transient do+ starts_at { nil }+ ends_at { nil }+ end+ end++ factory :volume_price_rule, class: Spree::PriceRules::VolumeRule do+ after(:build) do |rule, evaluator|+ rule.preferred_min_quantity = evaluator.min_quantity if evaluator.min_quantity.present?+ rule.preferred_max_quantity = evaluator.max_quantity if evaluator.max_quantity.present?+ rule.preferred_apply_to = evaluator.apply_to if evaluator.apply_to.present?+ end++ transient do+ min_quantity { nil }+ max_quantity { nil }+ apply_to { nil }+ end+ end++ factory :product_taxon_price_rule, class: Spree::PriceRules::ProductTaxonRule do+ after(:build) do |rule, evaluator|+ rule.preferred_taxon_ids = evaluator.taxon_ids if evaluator.respond_to?(:taxon_ids)+ rule.preferred_include_descendants = evaluator.include_descendants if evaluator.respond_to?(:include_descendants)+ end++ transient do+ taxon_ids { [] }+ include_descendants { true }+ end+ end++ factory :user_price_rule, class: Spree::PriceRules::UserRule do+ after(:build) do |rule, evaluator|+ rule.preferred_user_ids = evaluator.user_ids if evaluator.respond_to?(:user_ids)+ end++ transient do+ user_ids { [] }+ end+ end+ end+enddiff --git a/core/spec/lib/spree/pricing/resolver_spec.rb b/core/spec/lib/spree/pricing/resolver_spec.rbnew file mode 100644index 0000000000..93d1285847--- /dev/null+++ b/core/spec/lib/spree/pricing/resolver_spec.rb@@ -0,0 +1,152 @@+require 'spec_helper'++describe Spree::Pricing::Resolver do+ let(:variant) { create(:variant) }+ let(:store) { create(:store) }+ let(:currency) { 'USD' }+ let(:context) { Spree::Pricing::Context.new(variant: variant, currency: currency, store: store) }+ let(:resolver) { described_class.new(context) }++ # Clear cache before each test to avoid stale cached prices+ before do+ Rails.cache.clear+ end++ describe '#resolve' do+ context 'when no price lists exist' do+ it 'returns the base price' do+ base_price = variant.prices.base_prices.with_currency(currency).first+ price = resolver.resolve+ expect(price).to eq(base_price)+ expect(price.amount).to eq(19.99)+ expect(price.price_list_id).to be_nil+ end+ end++ context 'when no matching price list exists' do+ let!(:price_list) { create(:price_list, status: 'inactive') }+ let!(:list_price) { create(:price, variant: variant, currency: currency, amount: 15.99, price_list: price_list) }++ it 'falls back to base price' do+ base_price = variant.prices.base_prices.with_currency(currency).first+ price = resolver.resolve+ expect(price).to eq(base_price)+ expect(price.amount).to eq(19.99)+ end+ end++ context 'with applicable price list' do+ let!(:price_list) { create(:price_list, status: 'active', priority: 10) }+ let!(:list_price) { create(:price, variant: variant, currency: currency, amount: 15.99, price_list: price_list) }++ before do+ create(:store_price_rule, price_list: price_list, store_ids: [store.id])+ end++ it 'returns the price list price' do+ price = resolver.resolve+ expect(price).to eq(list_price)+ expect(price.amount).to eq(15.99)+ expect(price.price_list_id).to eq(price_list.id)+ end+ end++ context 'with multiple applicable price lists' do+ let!(:low_priority_list) { create(:price_list, status: 'active', priority: 10) }+ let!(:low_priority_price) { create(:price, variant: variant, currency: currency, amount: 17.99, price_list: low_priority_list) }++ let!(:high_priority_list) { create(:price_list, status: 'active', priority: 100) }+ let!(:high_priority_price) { create(:price, variant: variant, currency: currency, amount: 15.99, price_list: high_priority_list) }++ before do+ create(:store_price_rule, price_list: low_priority_list, store_ids: [store.id])+ create(:store_price_rule, price_list: high_priority_list, store_ids: [store.id])+ end++ it 'returns the highest priority price list price' do+ price = resolver.resolve+ expect(price).to eq(high_priority_price)+ expect(price.amount).to eq(15.99)+ expect(price.price_list_id).to eq(high_priority_list.id)+ end+ end++ context 'with date range price list' do+ let!(:price_list) do+ create(:price_list,+ status: 'active',+ starts_at: 1.day.ago,+ ends_at: 1.day.from_now)+ end+ let!(:list_price) { create(:price, variant: variant, currency: currency, amount: 15.99, price_list: price_list) }++ it 'returns price list price when within date range' do+ price = resolver.resolve+ expect(price).to eq(list_price)+ end++ it 'returns base price when outside date range' do+ base_price = variant.prices.base_prices.with_currency(currency).first+ Timecop.travel(2.days.from_now) do+ Rails.cache.clear # Clear cache for the new time context+ price = resolver.resolve+ expect(price).to eq(base_price)+ end+ end+ end++ context 'with volume-based pricing' do+ let!(:bulk_list) { create(:price_list, status: 'active', priority: 50) }+ let!(:bulk_price) { create(:price, variant: variant, currency: currency, amount: 8.00, price_list: bulk_list) }++ before do+ # Update the base price to 10.00 for this test+ variant.prices.base_prices.with_currency(currency).update_all(amount: 10.00)+ create(:volume_price_rule, price_list: bulk_list, min_quantity: 10)+ end++ it 'returns bulk price when quantity threshold met' do+ context_with_quantity = Spree::Pricing::Context.new(+ variant: variant,+ currency: currency,+ store: store,+ quantity: 10+ )+ resolver = described_class.new(context_with_quantity)+ price = resolver.resolve++ expect(price).to eq(bulk_price)+ expect(price.amount).to eq(8.00)+ end++ it 'returns base price when quantity threshold not met' do+ base_price = variant.prices.base_prices.with_currency(currency).first+ context_with_quantity = Spree::Pricing::Context.new(+ variant: variant,+ currency: currency,+ store: store,+ quantity: 5+ )+ resolver = described_class.new(context_with_quantity)+ price = resolver.resolve++ expect(price).to eq(base_price)+ expect(price.amount).to eq(10.00)+ end+ end++ context 'when no base price exists' do+ before do+ variant.prices.destroy_all+ end++ it 'returns a new unpersisted price object' do+ price = resolver.resolve+ expect(price).to be_a(Spree::Price)+ expect(price).not_to be_persisted+ expect(price.amount).to be_nil+ expect(price.currency).to eq(currency)+ end+ end+ end+enddiff --git a/core/spec/models/spree/price_list_spec.rb b/core/spec/models/spree/price_list_spec.rbnew file mode 100644index 0000000000..5b0392513a--- /dev/null+++ b/core/spec/models/spree/price_list_spec.rb@@ -0,0 +1,147 @@+require 'spec_helper'++describe Spree::PriceList, type: :model do+ describe 'associations' do+ it { is_expected.to have_many(:price_rules).dependent(:destroy) }+ it { is_expected.to have_many(:prices).dependent(:nullify) }+ end++ describe 'validations' do+ it { is_expected.to validate_presence_of(:name) }+ it { is_expected.to validate_presence_of(:priority) }+ it { is_expected.to validate_presence_of(:status) }+ it { is_expected.to validate_presence_of(:match_policy) }+ it { is_expected.to validate_inclusion_of(:status).in_array(%w[active inactive scheduled]) }+ it { is_expected.to validate_inclusion_of(:match_policy).in_array(%w[all any]) }++ context 'date range validation' do+ let(:price_list) { build(:price_list, starts_at: 2.days.from_now, ends_at: 1.day.from_now) }++ it 'validates starts_at is before ends_at' do+ expect(price_list).not_to be_valid+ expect(price_list.errors[:ends_at]).to include('must be after starts at')+ end+ end+ end++ describe 'scopes' do+ let!(:active_price_list) { create(:price_list, status: 'active') }+ let!(:inactive_price_list) { create(:price_list, status: 'inactive') }+ let!(:scheduled_price_list) { create(:price_list, status: 'scheduled') }+ let!(:high_priority_list) { create(:price_list, priority: 100) }+ let!(:low_priority_list) { create(:price_list, priority: 10) }++ describe '.active' do+ it 'returns only active price lists' do+ expect(described_class.active).to include(active_price_list)+ expect(described_class.active).not_to include(inactive_price_list)+ end+ end++ describe '.inactive' do+ it 'returns only inactive price lists' do+ expect(described_class.inactive).to include(inactive_price_list)+ expect(described_class.inactive).not_to include(active_price_list)+ end+ end++ describe '.scheduled' do+ it 'returns only scheduled price lists' do+ expect(described_class.scheduled).to include(scheduled_price_list)+ expect(described_class.scheduled).not_to include(active_price_list)+ end+ end++ describe '.by_priority' do+ it 'returns price lists ordered by priority descending' do+ ordered = described_class.by_priority.to_a+ high_index = ordered.index(high_priority_list)+ low_index = ordered.index(low_priority_list)+ expect(high_index).to be < low_index+ end+ end++ describe '.current' do+ let!(:past_list) { create(:price_list, ends_at: 1.day.ago) }+ let!(:future_list) { create(:price_list, starts_at: 1.day.from_now) }+ let!(:current_list) { create(:price_list, starts_at: 1.day.ago, ends_at: 1.day.from_now) }++ it 'returns only price lists within date range' do+ expect(described_class.current).to include(current_list)+ expect(described_class.current).not_to include(past_list, future_list)+ end+ end+ end++ describe '#applicable?' do+ let(:price_list) { create(:price_list, status: 'active') }+ let(:variant) { create(:variant) }+ let(:context) { Spree::Pricing::Context.new(variant: variant, currency: 'USD') }++ context 'when price list is inactive' do+ before { price_list.update(status: 'inactive') }++ it 'returns false' do+ expect(price_list.applicable?(context)).to be false+ end+ end++ context 'when price list is outside date range' do+ before { price_list.update(starts_at: 1.week.from_now, ends_at: 2.weeks.from_now) }++ it 'returns false' do+ expect(price_list.applicable?(context)).to be false+ end+ end++ context 'with rules and match_policy = all' do+ let!(:passing_rule) do+ rule = create(:store_price_rule, price_list: price_list)+ allow(rule).to receive(:applicable?).and_return(true)+ rule+ end+ let!(:failing_rule) do+ rule = create(:zone_price_rule, price_list: price_list)+ allow(rule).to receive(:applicable?).and_return(false)+ rule+ end++ before { price_list.update(match_policy: 'all') }++ it 'returns false if any rule fails' do+ expect(price_list.applicable?(context)).to be false+ end+ end++ context 'with rules and match_policy = any' do+ let!(:passing_rule) do+ rule = create(:store_price_rule, price_list: price_list)+ allow(rule).to receive(:applicable?).and_return(true)+ rule+ end+ let!(:failing_rule) do+ rule = create(:zone_price_rule, price_list: price_list)+ allow(rule).to receive(:applicable?).and_return(false)+ rule+ end++ before { price_list.update(match_policy: 'any') }++ it 'returns true if any rule passes' do+ expect(price_list.applicable?(context)).to be true+ end+ end+ end++ describe '#active?' do+ it 'returns true when status is active' do+ price_list = build(:price_list, status: 'active')+ expect(price_list.active?).to be true+ end++ it 'returns false when status is not active' do+ price_list = build(:price_list, status: 'inactive')+ expect(price_list.active?).to be false+ end+ end+enddiff --git a/core/spec/models/spree/price_rules/date_range_rule_spec.rb b/core/spec/models/spree/price_rules/date_range_rule_spec.rbnew file mode 100644index 0000000000..3766904b10--- /dev/null+++ b/core/spec/models/spree/price_rules/date_range_rule_spec.rb@@ -0,0 +1,31 @@+require 'spec_helper'++describe Spree::PriceRules::DateRangeRule, type: :model do+ let(:price_list) { create(:price_list) }+ let(:rule) { create(:date_range_price_rule, price_list: price_list) }+ let(:variant) { create(:variant) }++ describe '#applicable?' do+ context 'with starts_at and ends_at set' do+ before do+ rule.preferred_starts_at = 1.day.ago+ rule.preferred_ends_at = 1.day.from_now+ end++ it 'returns true when date is within range' do+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD', date: Time.current)+ expect(rule.applicable?(context)).to be true+ end++ it 'returns false when date is before range' do+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD', date: 2.days.ago)+ expect(rule.applicable?(context)).to be false+ end++ it 'returns false when date is after range' do+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD', date: 2.days.from_now)+ expect(rule.applicable?(context)).to be false+ end+ end+ end+enddiff --git a/core/spec/models/spree/price_rules/product_taxon_rule_spec.rb b/core/spec/models/spree/price_rules/product_taxon_rule_spec.rbnew file mode 100644index 0000000000..4a8a214fea--- /dev/null+++ b/core/spec/models/spree/price_rules/product_taxon_rule_spec.rb@@ -0,0 +1,35 @@+require 'spec_helper'++describe Spree::PriceRules::ProductTaxonRule, type: :model do+ let(:price_list) { create(:price_list) }+ let(:rule) { create(:product_taxon_price_rule, price_list: price_list) }+ let(:taxon) { create(:taxon) }+ let(:product) { create(:product, taxons: [taxon]) }+ let(:variant) { product.master }++ describe '#applicable?' do+ context 'when taxon_ids preference is empty' do+ before { rule.preferred_taxon_ids = [] }++ it 'returns true for any product' do+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD')+ expect(rule.applicable?(context)).to be true+ end+ end++ context 'when taxon_ids preference is set' do+ before { rule.preferred_taxon_ids = [taxon.id] }++ it 'returns true when product has matching taxon' do+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD')+ expect(rule.applicable?(context)).to be true+ end++ it 'returns false when product does not have matching taxon' do+ other_product = create(:product)+ context = Spree::Pricing::Context.new(variant: other_product.master, currency: 'USD')+ expect(rule.applicable?(context)).to be false+ end+ end+ end+enddiff --git a/core/spec/models/spree/price_rules/store_rule_spec.rb b/core/spec/models/spree/price_rules/store_rule_spec.rbnew file mode 100644index 0000000000..9739c3c048--- /dev/null+++ b/core/spec/models/spree/price_rules/store_rule_spec.rb@@ -0,0 +1,39 @@+require 'spec_helper'++describe Spree::PriceRules::StoreRule, type: :model do+ let(:price_list) { create(:price_list) }+ let(:rule) { create(:store_price_rule, price_list: price_list) }+ let(:store) { create(:store) }+ let(:variant) { create(:variant) }++ describe '#applicable?' do+ context 'when store_ids preference is empty' do+ before { rule.preferred_store_ids = [] }++ it 'returns true for any store' do+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD', store: store)+ expect(rule.applicable?(context)).to be true+ end+ end++ context 'when store_ids preference is set' do+ before { rule.preferred_store_ids = [store.id] }++ it 'returns true when context store matches' do+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD', store: store)+ expect(rule.applicable?(context)).to be true+ end++ it 'returns false when context store does not match' do+ other_store = create(:store)+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD', store: other_store)+ expect(rule.applicable?(context)).to be false+ end++ it 'returns false when context has no store' do+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD')+ expect(rule.applicable?(context)).to be false+ end+ end+ end+enddiff --git a/core/spec/models/spree/price_rules/user_rule_spec.rb b/core/spec/models/spree/price_rules/user_rule_spec.rbnew file mode 100644index 0000000000..86f6943de6--- /dev/null+++ b/core/spec/models/spree/price_rules/user_rule_spec.rb@@ -0,0 +1,39 @@+require 'spec_helper'++describe Spree::PriceRules::UserRule, type: :model do+ let(:price_list) { create(:price_list) }+ let(:rule) { create(:user_price_rule, price_list: price_list) }+ let(:user) { create(:user) }+ let(:variant) { create(:variant) }++ describe '#applicable?' do+ context 'when user_ids preference is empty' do+ before { rule.preferred_user_ids = [] }++ it 'returns true when user is present' do+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD', user: user)+ expect(rule.applicable?(context)).to be true+ end+ end++ context 'when user_ids preference is set' do+ before { rule.preferred_user_ids = [user.id] }++ it 'returns true when context user matches' do+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD', user: user)+ expect(rule.applicable?(context)).to be true+ end++ it 'returns false when context user does not match' do+ other_user = create(:user)+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD', user: other_user)+ expect(rule.applicable?(context)).to be false+ end++ it 'returns false when context has no user' do+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD')+ expect(rule.applicable?(context)).to be false+ end+ end+ end+enddiff --git a/core/spec/models/spree/price_rules/volume_rule_spec.rb b/core/spec/models/spree/price_rules/volume_rule_spec.rbnew file mode 100644index 0000000000..01cc6bd0b7--- /dev/null+++ b/core/spec/models/spree/price_rules/volume_rule_spec.rb@@ -0,0 +1,33 @@+require 'spec_helper'++describe Spree::PriceRules::VolumeRule, type: :model do+ let(:price_list) { create(:price_list) }+ let(:rule) { create(:volume_price_rule, price_list: price_list, min_quantity: 10) }+ let(:variant) { create(:variant) }++ describe '#applicable?' do+ it 'returns true when quantity meets minimum' do+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD', quantity: 10)+ expect(rule.applicable?(context)).to be true+ end++ it 'returns false when quantity is below minimum' do+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD', quantity: 5)+ expect(rule.applicable?(context)).to be false+ end++ context 'with max_quantity set' do+ before { rule.preferred_max_quantity = 50 }++ it 'returns true when quantity is within range' do+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD', quantity: 25)+ expect(rule.applicable?(context)).to be true+ end++ it 'returns false when quantity exceeds maximum' do+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD', quantity: 100)+ expect(rule.applicable?(context)).to be false+ end+ end+ end+enddiff --git a/core/spec/models/spree/price_rules/zone_rule_spec.rb b/core/spec/models/spree/price_rules/zone_rule_spec.rbnew file mode 100644index 0000000000..6637a68541--- /dev/null+++ b/core/spec/models/spree/price_rules/zone_rule_spec.rb@@ -0,0 +1,39 @@+require 'spec_helper'++describe Spree::PriceRules::ZoneRule, type: :model do+ let(:price_list) { create(:price_list) }+ let(:rule) { create(:zone_price_rule, price_list: price_list) }+ let(:zone) { create(:zone) }+ let(:variant) { create(:variant) }++ describe '#applicable?' do+ context 'when zone_ids preference is empty' do+ before { rule.preferred_zone_ids = [] }++ it 'returns true for any zone' do+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD', zone: zone)+ expect(rule.applicable?(context)).to be true+ end+ end++ context 'when zone_ids preference is set' do+ before { rule.preferred_zone_ids = [zone.id] }++ it 'returns true when context zone matches' do+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD', zone: zone)+ expect(rule.applicable?(context)).to be true+ end++ it 'returns false when context zone does not match' do+ other_zone = create(:zone)+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD', zone: other_zone)+ expect(rule.applicable?(context)).to be false+ end++ it 'returns false when context has no zone' do+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD')+ expect(rule.applicable?(context)).to be false+ end+ end+ end+enddiff --git a/storefront/app/helpers/spree/products_helper.rb b/storefront/app/helpers/spree/products_helper.rbindex 532bf5917f..2a6f6cf3d1 100644--- a/storefront/app/helpers/spree/products_helper.rb+++ b/storefront/app/helpers/spree/products_helper.rb@@ -15,8 +15,37 @@ module Spree]end+ # Builds a pricing context for a variant with current request context+ #+ # @param product_or_variant [Spree::Product, Spree::Variant] The product or variant to build context for+ # @param options [Hash] Optional overrides for context attributes+ # @option options [String] :currency Override the currency (defaults to current_currency)+ # @option options [Spree::Store] :store Override the store (defaults to current_store)+ # @option options [Spree::Zone] :zone Override the zone (defaults to current_order.tax_zone || current_store.checkout_zone)+ # @option options [Spree::User] :user Override the user (defaults to try_spree_current_user)+ # @option options [Integer] :quantity Specify quantity for volume pricing+ # @option options [Time] :date Specify date for date-based pricing+ # @option options [Spree::Order] :order Specify the order+ # @return [Spree::Pricing::Context] The pricing context+ def pricing_context_for_variant(product_or_variant, **options)+ variant = product_or_variant.is_a?(Spree::Product) ? product_or_variant.default_variant : product_or_variant++ Spree::Pricing::Context.new(+ variant: variant,+ currency: options[:currency] || current_currency,+ store: options[:store] || current_store,+ zone: options[:zone] || current_order&.tax_zone || current_store.checkout_zone,+ user: options[:user] || try_spree_current_user,+ quantity: options[:quantity],+ date: options[:date],+ order: options[:order] || current_order+ )+ end+def should_display_compare_at_price?(product_or_variant)- price = product_or_variant.price_in(current_currency)+ variant = product_or_variant.is_a?(Spree::Product) ? product_or_variant.default_variant : product_or_variant+ context = pricing_context_for_variant(variant)+ price = variant.price_for(context)price.compare_at_amount.present? && (price.compare_at_amount > price.amount)enddiff --git a/storefront/app/views/themes/default/spree/products/_add_to_cart_button.html.erb b/storefront/app/views/themes/default/spree/products/_add_to_cart_button.html.erbindex d1ba9862c8..72a4e76015 100644--- a/storefront/app/views/themes/default/spree/products/_add_to_cart_button.html.erb+++ b/storefront/app/views/themes/default/spree/products/_add_to_cart_button.html.erb@@ -5,11 +5,24 @@ options_param_name ||= :optionsnot_selected_options = product_not_selected_options(product, selected_variant, options_param_name: options_param_name)all_options_selected = (params[options_param_name].present? || @variant_from_options.present?) && not_selected_options.empty?not_all_options_selected = !all_options_selected && product.any_variant_available?(current_currency)++# Build pricing context for selected variant+if selected_variant+ variant_pricing_context = pricing_context_for_variant(selected_variant)+ variant_price = selected_variant.price_for(variant_pricing_context)+else+ variant_price = nil+end+variant_not_available = selected_variant.nil? ||product.discontinued? ||selected_variant.discontinued? ||!selected_variant.purchasable? ||- selected_variant.price_in(current_currency).amount.nil?+ variant_price&.amount.nil?++# Build pricing context for product+product_pricing_context = pricing_context_for_variant(product)+product_price = product.default_variant.price_for(product_pricing_context)%><div class='<%= sticky_button_classes %> bottom-0 flex flex-col gap-4 z-10' data-sticky-button-target='stickyButton'>@@ -22,7 +35,7 @@ variant_not_available = selected_variant.nil? ||].compact.join(' '),product_form_target: 'submit'},- disabled: product.price_in(current_currency).zero? do %>+ disabled: product_price.zero? do %><% if not_selected_options.size == 1 %><span><%= Spree.t('storefront.variant_picker.please_choose', option_type: not_selected_options[0].presentation) %></span><% elsif not_all_options_selected %>@@ -47,7 +60,7 @@ variant_not_available = selected_variant.nil? ||].compact.join(' '),product_form_target: 'submit'},- disabled: product.price_in(current_currency).zero? do %>+ disabled: product_price.zero? do %><% if not_selected_options.size == 1 %><span><%= Spree.t('storefront.variant_picker.please_choose', option_type: not_selected_options[0].presentation) %></span><% elsif not_all_options_selected %>diff --git a/storefront/app/views/themes/default/spree/products/_price.html.erb b/storefront/app/views/themes/default/spree/products/_price.html.erbindex 0f79211152..d0a52199ff 100644--- a/storefront/app/views/themes/default/spree/products/_price.html.erb+++ b/storefront/app/views/themes/default/spree/products/_price.html.erb@@ -20,7 +20,9 @@target ||= product- price = target.price_in(current_currency)+ variant_for_context = target.is_a?(Spree::Product) ? target.default_variant : target+ pricing_context = pricing_context_for_variant(variant_for_context)+ price = variant_for_context.price_for(pricing_context)money_price = price.display_amountif target.is_a?(Spree::Product) && !use_variant && product.price_varies?(current_currency)diff --git a/storefront/spec/helpers/spree/products_helper_spec.rb b/storefront/spec/helpers/spree/products_helper_spec.rbnew file mode 100644index 0000000000..1b764efb79--- /dev/null+++ b/storefront/spec/helpers/spree/products_helper_spec.rb@@ -0,0 +1,143 @@+require 'spec_helper'++describe Spree::ProductsHelper, type: :helper do+ let(:store) { create(:store) }+ let(:zone) { create(:zone) }+ let(:user) { create(:user) }+ let(:variant) { create(:variant) }+ let(:product) { variant.product }+ let(:order) { create(:order, store: store, user: user) }+ let(:currency) { 'USD' }++ before do+ allow(helper).to receive(:current_store).and_return(store)+ allow(helper).to receive(:current_currency).and_return(currency)+ allow(helper).to receive(:try_spree_current_user).and_return(user)+ allow(helper).to receive(:current_order).and_return(order)+ end++ describe '#pricing_context_for_variant' do+ context 'with a variant' do+ it 'builds a pricing context with default values' do+ context = helper.pricing_context_for_variant(variant)++ expect(context).to be_a(Spree::Pricing::Context)+ expect(context.variant).to eq(variant)+ expect(context.currency).to eq(currency)+ expect(context.store).to eq(store)+ expect(context.user).to eq(user)+ expect(context.order).to eq(order)+ end++ it 'uses order.tax_zone when available' do+ order.update(ship_address: create(:address))+ tax_zone = create(:zone)+ allow(order).to receive(:tax_zone).and_return(tax_zone)++ context = helper.pricing_context_for_variant(variant)++ expect(context.zone).to eq(tax_zone)+ end++ it 'falls back to store.checkout_zone when order has no tax_zone' do+ allow(order).to receive(:tax_zone).and_return(nil)+ allow(store).to receive(:checkout_zone).and_return(zone)++ context = helper.pricing_context_for_variant(variant)++ expect(context.zone).to eq(zone)+ end+ end++ context 'with a product' do+ it 'builds a pricing context using the default variant' do+ context = helper.pricing_context_for_variant(product)++ expect(context).to be_a(Spree::Pricing::Context)+ expect(context.variant).to eq(product.default_variant)+ end+ end++ context 'with custom options' do+ let(:custom_currency) { 'EUR' }+ let(:custom_store) { create(:store) }+ let(:custom_zone) { create(:zone) }+ let(:custom_user) { create(:user) }+ let(:custom_order) { create(:order) }+ let(:custom_quantity) { 5 }+ let(:custom_date) { Time.current }++ it 'overrides default values with provided options' do+ context = helper.pricing_context_for_variant(+ variant,+ currency: custom_currency,+ store: custom_store,+ zone: custom_zone,+ user: custom_user,+ order: custom_order,+ quantity: custom_quantity,+ date: custom_date+ )++ expect(context.variant).to eq(variant)+ expect(context.currency).to eq(custom_currency)+ expect(context.store).to eq(custom_store)+ expect(context.zone).to eq(custom_zone)+ expect(context.user).to eq(custom_user)+ expect(context.order).to eq(custom_order)+ expect(context.quantity).to eq(custom_quantity)+ expect(context.date).to eq(custom_date)+ end++ it 'allows partial overrides' do+ context = helper.pricing_context_for_variant(+ variant,+ quantity: custom_quantity+ )++ expect(context.variant).to eq(variant)+ expect(context.currency).to eq(currency)+ expect(context.store).to eq(store)+ expect(context.user).to eq(user)+ expect(context.quantity).to eq(custom_quantity)+ end+ end+ end++ describe '#should_display_compare_at_price?' do+ let(:variant) { create(:variant, price: 10, compare_at_price: 15) }++ before do+ allow(helper).to receive(:current_order).and_return(nil)+ allow(store).to receive(:checkout_zone).and_return(zone)+ end++ context 'when compare_at_price is greater than price' do+ it 'returns true' do+ expect(helper.should_display_compare_at_price?(variant)).to be true+ end+ end++ context 'when compare_at_price is not present' do+ before { variant.update(compare_at_price: nil) }++ it 'returns false' do+ expect(helper.should_display_compare_at_price?(variant)).to be false+ end+ end++ context 'when compare_at_price is less than or equal to price' do+ before { variant.update(compare_at_price: 8) }++ it 'returns false' do+ expect(helper.should_display_compare_at_price?(variant)).to be false+ end+ end++ context 'with a product' do+ it 'uses the default variant' do+ expect(helper.should_display_compare_at_price?(product)).to be true+ end+ end+ end+end