Sha256: 7afa5c237fc78f7526835ac259e9f957a4a0f97bef483d539bcd8446ad884210

Contents?: true

Size: 1.82 KB

Versions: 5

Compression:

Stored size: 1.82 KB

Contents

module Spree
  class OrderContents
    attr_accessor :order, :currency

    def initialize(order)
      @order = order
    end

    def add(variant, quantity, currency=nil, shipment=nil)
      #get current line item for variant if exists
      line_item = order.find_line_item_by_variant(variant)

      #add variant qty to line_item
      add_to_line_item(line_item, variant, quantity, currency, shipment)
    end

    def remove(variant, quantity, shipment=nil)
      #get current line item for variant
      line_item = order.find_line_item_by_variant(variant)

      #TODO raise exception if line_item is nil

      #remove variant qty from line_item
      remove_from_line_item(line_item, variant, quantity, shipment)
    end

    private

    def add_to_line_item(line_item, variant, quantity, currency=nil, shipment=nil)
      if line_item
        line_item.target_shipment = shipment
        line_item.quantity += quantity
        line_item.currency = currency unless currency.nil?
        line_item.save
      else
        line_item = LineItem.new(quantity: quantity)
        line_item.target_shipment = shipment
        line_item.variant = variant
        if currency
          line_item.currency = currency unless currency.nil?
          line_item.price    = variant.price_in(currency).amount
        else
          line_item.price    = variant.price
        end
        order.line_items << line_item
        line_item
      end

      order.reload
      line_item
    end

    def remove_from_line_item(line_item, variant, quantity, shipment=nil)
      line_item.quantity += -quantity
      line_item.target_shipment= shipment

      if line_item.quantity == 0
        Spree::OrderInventory.new(order).verify(line_item, shipment)
        line_item.destroy
      else
        line_item.save!
      end

      order.reload
      line_item
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
spree_core-2.0.3 app/models/spree/order_contents.rb
spree_core-2.0.2 app/models/spree/order_contents.rb
spree_core-2.0.1 app/models/spree/order_contents.rb
spree_core-2.0.0 app/models/spree/order_contents.rb
spree_core-2.0.0.rc1 app/models/spree/order_contents.rb