Sha256: 00c4c2246258f720fd868f7bd737a0a0608039d2f11506a872926cf8a766b97e

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

module Spree
  class Promotion::Actions::CreateLineItems < PromotionAction
    has_many :promotion_action_line_items, :foreign_key => 'promotion_action_id'

    attr_accessor :line_items_string

    def perform(options = {})
      return unless order = options[:order]
      promotion_action_line_items.each do |item|
        current_quantity = order.quantity_of(item.variant)
        if current_quantity < item.quantity
          order.add_variant(item.variant, item.quantity - current_quantity)
        end
      end
    end

    def line_items_string
      promotion_action_line_items.map { |li| "#{li.variant_id}x#{li.quantity}" }.join(',')
    end

    def line_items_string=(value)
      promotion_action_line_items.destroy_all
      value.to_s.split(',').each do |str|
        variant_id, quantity = str.split('x')
        if variant_id && quantity && variant = Variant.find_by_id(variant_id)
          promotion_action_line_items.create({
            :variant => variant,
            :quantity => quantity.to_i,
          }, :without_protection => true)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spree_promo-1.0.7 app/models/spree/promotion/actions/create_line_items.rb
spree_promo-1.0.6 app/models/spree/promotion/actions/create_line_items.rb