Sha256: c6090400ec2923918883b5e01ef7c92d2428f82c86ee54a1af7d355948bbf2b4

Contents?: true

Size: 965 Bytes

Versions: 4

Compression:

Stored size: 965 Bytes

Contents

# frozen_string_literal: true

module SolidusFriendlyPromotions
  module Conditions
    class FirstRepeatPurchaseSince < Condition
      include OrderLevelCondition

      preference :days_ago, :integer, default: 365
      validates :preferred_days_ago, numericality: {only_integer: true, greater_than: 0}

      # 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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
solidus_friendly_promotions-1.9.3 app/models/solidus_friendly_promotions/conditions/first_repeat_purchase_since.rb
solidus_friendly_promotions-1.9.2 app/models/solidus_friendly_promotions/conditions/first_repeat_purchase_since.rb
solidus_friendly_promotions-1.9.1 app/models/solidus_friendly_promotions/conditions/first_repeat_purchase_since.rb
solidus_friendly_promotions-1.9.0 app/models/solidus_friendly_promotions/conditions/first_repeat_purchase_since.rb