Class: Spree::LineItem

Inherits:
Base
  • Object
show all
Defined in:
app/models/spree/line_item.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize_preference_defaults, page, preference

Methods included from Preferences::Preferable

#default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference

Instance Attribute Details

#target_shipmentObject

Returns the value of attribute target_shipment.



43
44
45
# File 'app/models/spree/line_item.rb', line 43

def target_shipment
  @target_shipment
end

Instance Method Details

#amountBigDecimal Also known as: subtotal

Returns the amount of this line item, which is the line item’s price multiplied by its quantity.

Returns:

  • (BigDecimal)

    the amount of this line item, which is the line item’s price multiplied by its quantity.



68
69
70
# File 'app/models/spree/line_item.rb', line 68

def amount
  price * quantity
end

#copy_priceObject

Sets this line item’s price, cost price, and currency from this line item’s variant if they are nil and a variant is present.



50
51
52
53
54
55
56
# File 'app/models/spree/line_item.rb', line 50

def copy_price
  if variant
    self.price = variant.price if price.nil?
    self.cost_price = variant.cost_price if cost_price.nil?
    self.currency = variant.currency if currency.nil?
  end
end

#copy_tax_categoryObject

Sets this line item’s tax category from this line item’s variant if a variant is present.



60
61
62
63
64
# File 'app/models/spree/line_item.rb', line 60

def copy_tax_category
  if variant
    self.tax_category = variant.tax_category
  end
end

#discounted_amountBigDecimal

Returns the amount of this line item, taking into consideration line item promotions.

Returns:

  • (BigDecimal)

    the amount of this line item, taking into consideration line item promotions.



75
76
77
# File 'app/models/spree/line_item.rb', line 75

def discounted_amount
  amount + promo_total
end

#discounted_moneySpree::Money

Returns the amount of this line item, taking into consideration line item promotions.

Returns:

  • (Spree::Money)

    the amount of this line item, taking into consideration line item promotions.



81
82
83
# File 'app/models/spree/line_item.rb', line 81

def discounted_money
  Spree::Money.new(discounted_amount, { currency: currency })
end

#final_amountBigDecimal Also known as: total

Returns the amount of this line item, taking into consideration all its adjustments.

Returns:

  • (BigDecimal)

    the amount of this line item, taking into consideration all its adjustments.



87
88
89
# File 'app/models/spree/line_item.rb', line 87

def final_amount
  amount + adjustment_total
end

#insufficient_stock?Boolean

Returns true when it is not possible to supply the required quantity of stock of this line item’s variant.

Returns:

  • (Boolean)

    true when it is not possible to supply the required quantity of stock of this line item’s variant



118
119
120
# File 'app/models/spree/line_item.rb', line 118

def insufficient_stock?
  !sufficient_stock?
end

#invalid_quantity_checkObject

Sets the quantity to zero if it is nil or less than zero.



106
107
108
# File 'app/models/spree/line_item.rb', line 106

def invalid_quantity_check
  self.quantity = 0 if quantity.nil? || quantity < 0
end

#moneySpree::Moeny Also known as: display_total, display_amount

Returns the amount of this line item.

Returns:

  • (Spree::Moeny)

    the amount of this line item



99
100
101
# File 'app/models/spree/line_item.rb', line 99

def money
  Spree::Money.new(amount, { currency: currency })
end

#options=(options = {}) ⇒ Object

Sets the options on the line item according to the order’s currency or one passed in.

Parameters:

  • options (Hash) (defaults to: {})

    options for this line item



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/models/spree/line_item.rb', line 126

def options=(options={})
  return unless options.present?

  opts = options.dup # we will be deleting from the hash, so leave the caller's copy intact

  currency = opts.delete(:currency) || order.try(:currency)

  if currency
    self.currency = currency
    self.price    = variant.price_in(currency).amount +
                    variant.price_modifier_amount_in(currency, opts)
  else
    self.price    = variant.price +
                    variant.price_modifier_amount(opts)
  end

  self.assign_attributes opts
end

#productSpree::Product?

Note:

This will return the product even if it has been deleted.

Returns the product associated with this line item, if there is one.

Returns:

  • (Spree::Product, nil)

    the product associated with this line item, if there is one



40
# File 'app/models/spree/line_item.rb', line 40

delegate :product, to: :variant

#single_moneySpree::Money Also known as: single_display_amount

Returns the price of this line item.

Returns:



93
94
95
# File 'app/models/spree/line_item.rb', line 93

def single_money
  Spree::Money.new(price, { currency: currency })
end

#sufficient_stock?Boolean

Returns true when it is possible to supply the required quantity of stock of this line item’s variant.

Returns:

  • (Boolean)

    true when it is possible to supply the required quantity of stock of this line item’s variant



112
113
114
# File 'app/models/spree/line_item.rb', line 112

def sufficient_stock?
  Stock::Quantifier.new(variant).can_supply? quantity
end