Sha256: 70a13b1dff0612aeaf2617aee8248f2ac4264dbf4f66ec7da98118e0ee99f7fb

Contents?: true

Size: 1.8 KB

Versions: 62

Compression:

Stored size: 1.8 KB

Contents

module Workarea
  module Pricing
    class Discount
      # This discount allows discounting items based on whether any of the
      # the {Catalog::Product} or {Catalog::Variant} details
      # match.
      #
      class ProductAttribute < Discount
        include FlatOrPercentOff
        include Conditions::OrderTotal
        include Conditions::PromoCodes
        include Conditions::ItemQuantity
        include Conditions::UserTags

        # @!attribute attribute_name
        #   @return [Array] the details name to match
        #
        field :attribute_name, type: String

        # @!attribute attribute_value
        #   @return [Array] the details value to match
        #
        field :attribute_value, type: String

        validates :attribute_name, presence: true
        validates :attribute_value, 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 for whether any items in the order
        # match the attribute qualification.
        #
        # @param [Workarea::Order]
        # @return [Boolean]
        #
        def item_qualifies?(item)
          super && ItemQualifier.new(self, item).qualifies?
        end

        # Create the discount price adjustments on items that match
        # the attribute requirements.
        #
        # @param [Pricing::Discount::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
        end
      end
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/models/workarea/pricing/discount/product_attribute.rb
workarea-core-3.5.26 app/models/workarea/pricing/discount/product_attribute.rb
workarea-core-3.4.45 app/models/workarea/pricing/discount/product_attribute.rb
workarea-core-3.5.25 app/models/workarea/pricing/discount/product_attribute.rb
workarea-core-3.5.23 app/models/workarea/pricing/discount/product_attribute.rb
workarea-core-3.4.44 app/models/workarea/pricing/discount/product_attribute.rb
workarea-core-3.5.22 app/models/workarea/pricing/discount/product_attribute.rb
workarea-core-3.4.43 app/models/workarea/pricing/discount/product_attribute.rb
workarea-core-3.5.21 app/models/workarea/pricing/discount/product_attribute.rb
workarea-core-3.4.42 app/models/workarea/pricing/discount/product_attribute.rb
workarea-core-3.5.20 app/models/workarea/pricing/discount/product_attribute.rb
workarea-core-3.4.41 app/models/workarea/pricing/discount/product_attribute.rb
workarea-core-3.5.19 app/models/workarea/pricing/discount/product_attribute.rb
workarea-core-3.4.40 app/models/workarea/pricing/discount/product_attribute.rb
workarea-core-3.5.18 app/models/workarea/pricing/discount/product_attribute.rb
workarea-core-3.4.39 app/models/workarea/pricing/discount/product_attribute.rb
workarea-core-3.5.17 app/models/workarea/pricing/discount/product_attribute.rb
workarea-core-3.4.38 app/models/workarea/pricing/discount/product_attribute.rb
workarea-core-3.5.16 app/models/workarea/pricing/discount/product_attribute.rb
workarea-core-3.4.37 app/models/workarea/pricing/discount/product_attribute.rb