Sha256: d460531b199d4ba73f8017124344f088717e6af65d09166f215d3d58e9fdb5e2

Contents?: true

Size: 1.6 KB

Versions: 62

Compression:

Stored size: 1.6 KB

Contents

module Workarea
  module Pricing
    class Discount
      class Product < Discount
        # This discount allows discounting individual products.
        #
        include FlatOrPercentOff
        include Conditions::OrderTotal
        include Conditions::PromoCodes
        include Conditions::ItemQuantity
        include Conditions::UserTags

        # @!attribute product_ids
        #   @return [Array] discount applies to these {Catalog::Product} ids
        #
        field :product_ids, type: Array, default: []
        list_field :product_ids
        validates :product_ids, presence: true

        # @private
        def self.model_name
          Discount.model_name
        end

        # Price changes apply at the item level
        #
        # @return [String]
        #
        self.price_level = 'item'

        # Qualifier method to check whether any items in this order
        # qualify for this discount.
        #
        # @param [Workarea::Order::Item]
        # @return [Boolean]
        #
        def item_qualifies?(item)
          super && item.product_id.in?(product_ids)
        end

        # Creates the price adjustments for the discount on the matching
        # items.
        #
        # @param [Workarea::Order]
        # @return [Workarea::Order]
        #
        def apply(order)
          order.items.each do |item|
            next unless item_qualifies?(item)

            value = amount_calculator.calculate(item.current_unit_price, item.quantity)
            item.adjust_pricing(adjustment_data(value, item.quantity))
          end

          order
        end
      end
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.4.27 app/models/workarea/pricing/discount/product.rb
workarea-core-3.5.4 app/models/workarea/pricing/discount/product.rb
workarea-core-3.4.26 app/models/workarea/pricing/discount/product.rb
workarea-core-3.5.3 app/models/workarea/pricing/discount/product.rb
workarea-core-3.4.25 app/models/workarea/pricing/discount/product.rb
workarea-core-3.5.2 app/models/workarea/pricing/discount/product.rb
workarea-core-3.4.24 app/models/workarea/pricing/discount/product.rb
workarea-core-3.5.1 app/models/workarea/pricing/discount/product.rb
workarea-core-3.4.23 app/models/workarea/pricing/discount/product.rb
workarea-core-3.4.22 app/models/workarea/pricing/discount/product.rb
workarea-core-3.5.0 app/models/workarea/pricing/discount/product.rb
workarea-core-3.4.21 app/models/workarea/pricing/discount/product.rb
workarea-core-3.5.0.beta.1 app/models/workarea/pricing/discount/product.rb
workarea-core-3.4.20 app/models/workarea/pricing/discount/product.rb
workarea-core-3.4.19 app/models/workarea/pricing/discount/product.rb
workarea-core-3.4.18 app/models/workarea/pricing/discount/product.rb
workarea-core-3.4.17 app/models/workarea/pricing/discount/product.rb
workarea-core-3.4.16 app/models/workarea/pricing/discount/product.rb
workarea-core-3.4.15 app/models/workarea/pricing/discount/product.rb
workarea-core-3.4.14 app/models/workarea/pricing/discount/product.rb