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.

Improve handling scheduled Price Lists

Damian Legawiec720081b27cb8
diff --git a/core/app/models/spree/price_list.rb b/core/app/models/spree/price_list.rb
index a338506539..258d4ca0e0 100644
--- a/core/app/models/spree/price_list.rb
+++ b/core/app/models/spree/price_list.rb
@@ -58,10 +58,13 @@ module Spree
end
end
+ # Returns price lists applicable for a given pricing context
+ # - active: always applies (within date range)
+ # - scheduled: applies only within starts_at/ends_at date range
def self.for_context(context)
timezone = context.store&.preferred_timezone || 'UTC'
for_store(context.store)
- .with_status(:active)
+ .with_status(:active, :scheduled)
.current(timezone)
.by_position
end
@@ -94,6 +97,15 @@ module Spree
active? || scheduled?
end
+ # Returns true if the price list is currently in effect
+ # (active, or scheduled and within date range)
+ def currently_active?
+ return true if active? && within_date_range?(Time.current)
+ return true if scheduled? && within_date_range?(Time.current)
+
+ false
+ end
+
def add_products(product_ids)
return if product_ids.blank?
@@ -203,8 +215,11 @@ module Spree
end
def within_date_range?(date)
- return false if starts_at.present? && date < starts_at
- return false if ends_at.present? && date > ends_at
+ timezone = store&.preferred_timezone || Rails.application.config.time_zone
+ date_in_tz = date.in_time_zone(timezone)
+
+ return false if starts_at.present? && date_in_tz < starts_at
+ return false if ends_at.present? && date_in_tz > ends_at
true
end
diff --git a/core/lib/spree/testing_support/factories/price_list_factory.rb b/core/lib/spree/testing_support/factories/price_list_factory.rb
index 588a8267f4..ad1c9df719 100644
--- a/core/lib/spree/testing_support/factories/price_list_factory.rb
+++ b/core/lib/spree/testing_support/factories/price_list_factory.rb
@@ -18,6 +18,8 @@ FactoryBot.define do
trait :scheduled do
status { 'scheduled' }
+ starts_at { 1.hour.from_now }
+ ends_at { 1.week.from_now }
end
trait :with_date_range do