Sha256: 40a5e0aebee82a681283841929c031d64e079375e74168ba8934a6ca3d2907b2

Contents?: true

Size: 1.11 KB

Versions: 133

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

module Spree
  class Promotion < Spree::Base
    module Rules
      class FirstRepeatPurchaseSince < PromotionRule
        preference :days_ago, :integer, default: 365
        validates :preferred_days_ago, numericality: { only_integer: true, greater_than: 0 }

        # This promotion is applicable to orders only.
        def applicable?(promotable)
          promotable.is_a?(Spree::Order)
        end

        # This is never eligible if the order does not have a user, and that user does not have any previous completed orders.
        #
        # This is eligible if the user's most recently completed order is more than the preferred days ago
        # @param order [Spree::Order]
        def eligible?(order, _options = {})
          return false unless order.user

          last_order = last_completed_order(order.user)
          return false unless last_order

          last_order.completed_at < preferred_days_ago.days.ago
        end

        private

        def last_completed_order(user)
          user.orders.complete.order(:completed_at).last
        end
      end
    end
  end
end

Version data entries

133 entries across 133 versions & 3 rubygems

Version Path
solidus_legacy_promotions-4.4.2 app/models/spree/promotion/rules/first_repeat_purchase_since.rb
solidus_legacy_promotions-4.4.1 app/models/spree/promotion/rules/first_repeat_purchase_since.rb
solidus_legacy_promotions-4.4.0 app/models/spree/promotion/rules/first_repeat_purchase_since.rb
solidus_core-4.3.4 app/models/spree/promotion/rules/first_repeat_purchase_since.rb
solidus_core-4.2.4 app/models/spree/promotion/rules/first_repeat_purchase_since.rb
solidus_core-4.1.5 app/models/spree/promotion/rules/first_repeat_purchase_since.rb
solidus_core-4.3.3 app/models/spree/promotion/rules/first_repeat_purchase_since.rb
solidus_core-4.3.2 app/models/spree/promotion/rules/first_repeat_purchase_since.rb
solidus_core-4.1.4 app/models/spree/promotion/rules/first_repeat_purchase_since.rb
solidus_core-4.3.1 app/models/spree/promotion/rules/first_repeat_purchase_since.rb
solidus_core-4.3.0 app/models/spree/promotion/rules/first_repeat_purchase_since.rb
solidus_core-4.2.3 app/models/spree/promotion/rules/first_repeat_purchase_since.rb
solidus_core-4.1.3 app/models/spree/promotion/rules/first_repeat_purchase_since.rb
solidus_core-4.0.4 app/models/spree/promotion/rules/first_repeat_purchase_since.rb
solidus_core-3.4.6 app/models/spree/promotion/rules/first_repeat_purchase_since.rb
solidus_core-4.0.3 app/models/spree/promotion/rules/first_repeat_purchase_since.rb
solidus_core-4.1.2 app/models/spree/promotion/rules/first_repeat_purchase_since.rb
solidus_core-4.2.2 app/models/spree/promotion/rules/first_repeat_purchase_since.rb
solidus_core-3.4.5 app/models/spree/promotion/rules/first_repeat_purchase_since.rb
solidus_core-4.2.1 app/models/spree/promotion/rules/first_repeat_purchase_since.rb