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.

Automatically set the `starts_at` of Promotion

Damian Legawiecfa1bdf416cd5
diff --git a/core/app/models/spree/promotion.rb b/core/app/models/spree/promotion.rb
index f80cc8f24b..4dff892f42 100644
--- a/core/app/models/spree/promotion.rb
+++ b/core/app/models/spree/promotion.rb
@@ -37,7 +37,7 @@ module Spree
has_many :orders, through: :order_promotions, class_name: 'Spree::Order'
has_many :store_promotions, class_name: 'Spree::StorePromotion'
has_many :stores, class_name: 'Spree::Store', through: :store_promotions
- accepts_nested_attributes_for :promotion_actions, :promotion_rules
+ accepts_nested_attributes_for :promotion_actions, :promotion_rules, allow_destroy: true
#
# Callbacks
@@ -47,6 +47,7 @@ module Spree
before_validation :set_usage_limit_to_nil, if: -> { multi_codes? }
before_validation :set_kind
before_validation :downcase_code, if: -> { code.present? }
+ before_validation :set_starts_at_to_current_time, if: -> { starts_at.blank? }
after_commit :generate_coupon_codes, if: -> { multi_codes? }, on: [:create, :update]
after_commit :remove_coupons, unless: -> { multi_codes? }, on: :update
before_destroy :not_used?
@@ -307,6 +308,10 @@ module Spree
self.multi_codes = false
end
+ def set_starts_at_to_current_time
+ self.starts_at = Time.current
+ end
+
def generate_coupon_codes
return if number_of_codes.nil?
return if number_of_codes <= coupon_codes.count