Class: Spree::LineItem

Inherits:
Base
  • Object
show all
Extended by:
DisplayMoney
Includes:
Metadata
Defined in:
app/models/spree/line_item.rb

Overview

Variants placed in the Order at a particular price.

‘Spree::LineItem` is an ActiveRecord model which records which `Spree::Variant` a customer has chosen to place in their order. It also acts as the permanent record of the customer’s order by recording relevant price, taxation, and inventory concerns. Line items can also have adjustments placed on them as part of the promotion system.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DisplayMoney

money_methods

Methods inherited from Base

display_includes

Methods included from Core::Permalinks

#generate_permalink, #save_permalink

Instance Attribute Details

#price_currencyObject

Returns the value of attribute price_currency.



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

def price_currency
  @price_currency
end

#target_shipmentObject

Returns the value of attribute target_shipment.



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

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.



51
52
53
# File 'app/models/spree/line_item.rb', line 51

def amount
  price * quantity
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



108
109
110
# File 'app/models/spree/line_item.rb', line 108

def insufficient_stock?
  !sufficient_stock?
end

#money_price=(money) ⇒ Object

Sets price from a ‘Spree::Money` object

Parameters:

  • money (Spree::Money)
    • the money object to obtain price from



91
92
93
94
95
96
97
98
# File 'app/models/spree/line_item.rb', line 91

def money_price=(money)
  if !money
    self.price = nil
  else
    self.price_currency = money.currency.iso_code
    self.price = money.to_d
  end
end

#options=(options = {}) ⇒ Object

Sets options on the line item and updates the price.

The options can be arbitrary attributes on the LineItem.

Parameters:

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

    options for this line item



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/models/spree/line_item.rb', line 117

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

  assign_attributes options

  # When price is part of the options we are not going to fetch
  # it from the variant. Please note that this always allows to set
  # a price for this line item, even if there is no existing price
  # for the associated line item in the order currency.
  unless options.key?(:price) || options.key?("price")
    self.money_price = variant.price_for_options(pricing_options)&.money
  end
end

#pricing_optionsObject



140
141
142
# File 'app/models/spree/line_item.rb', line 140

def pricing_options
  Spree::Config.pricing_options_class.from_line_item(self)
end

#recalculate_priceObject

Recalculate the price using the pricing options for this line item. Useful for making sure carts are up-to-date when prices change. Will not make changes to completed orders.



135
136
137
138
# File 'app/models/spree/line_item.rb', line 135

def recalculate_price
  return if order.completed?
  self.money_price = variant.price_for_options(pricing_options)&.money
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



102
103
104
# File 'app/models/spree/line_item.rb', line 102

def sufficient_stock?
  Spree::Config.stock.quantifier_class.new(variant).can_supply? quantity
end

#tax_categorySpree::TaxCategory

This returns the variant’s tax category if the tax category ID on the line_item is nil. It looks like an association, but really is an override.

Returns:



149
150
151
# File 'app/models/spree/line_item.rb', line 149

def tax_category
  super || variant_tax_category
end

#tax_category_idInteger

This returns the variant’s tax category ID if the tax category ID on the line_id is nil. It looks like an association, but really is an override.

Returns:

  • (Integer)

    the variant’s tax category ID



158
159
160
# File 'app/models/spree/line_item.rb', line 158

def tax_category_id
  super || variant_tax_category_id
end

#totalBigDecimal

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.



58
59
60
# File 'app/models/spree/line_item.rb', line 58

def total
  amount + adjustment_total
end

#total_before_taxBigDecimal

Returns the amount of this item, taking into consideration all non-tax adjustments.

Returns:

  • (BigDecimal)

    the amount of this item, taking into consideration all non-tax adjustments.



64
65
66
# File 'app/models/spree/line_item.rb', line 64

def total_before_tax
  amount + adjustments.reject(&:tax?).sum(&:amount)
end

#total_excluding_vatBigDecimal

Note:

just like ‘amount`, this does not include any additional tax

Returns the amount of this line item before VAT tax.

Returns:

  • (BigDecimal)

    the amount of this line item before VAT tax



70
71
72
# File 'app/models/spree/line_item.rb', line 70

def total_excluding_vat
  total_before_tax - included_tax_total
end