Sha256: 57b5850f3cf38b1e643d984167f263eb3165c1222f4decb2afec5eb6e047792f

Contents?: true

Size: 890 Bytes

Versions: 2

Compression:

Stored size: 890 Bytes

Contents

# frozen_string_literal: true

module Spree
  class Promotion < Spree::Base
    module Rules
      class FirstOrder < PromotionRule
        attr_reader :user, :email

        def applicable?(promotable)
          promotable.is_a?(Spree::Order)
        end

        def eligible?(order, options = {})
          @user = order.try(:user) || options[:user]
          @email = order.email

          if (user || email) && (completed_orders.present? && completed_orders.first != order)
            eligibility_errors.add(:base, eligibility_error_message(:not_first_order), error_code: :not_first_order)
          end

          eligibility_errors.empty?
        end

        private

        def completed_orders
          user ? user.orders.complete : orders_by_email
        end

        def orders_by_email
          Spree::Order.where(email:).complete
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
solidus_legacy_promotions-4.4.1 app/models/spree/promotion/rules/first_order.rb
solidus_legacy_promotions-4.4.0 app/models/spree/promotion/rules/first_order.rb