Class: Spree::LineItem

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DisplayMoney

money_methods

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.



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

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.



47
48
49
# File 'app/models/spree/line_item.rb', line 47

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



54
55
56
# File 'app/models/spree/line_item.rb', line 54

def discounted_amount
  amount + promo_total
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.



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

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



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

def insufficient_stock?
  !sufficient_stock?
end

#money_price=(money) ⇒ Object

Sets price and currency from a ‘Spree::Money` object

Parameters:

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



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

def money_price=(money)
  self.price = money.to_d
  self.currency = money.currency.iso_code
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



110
111
112
113
114
115
116
117
118
119
# File 'app/models/spree/line_item.rb', line 110

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

  assign_attributes options

  # There's no need to call a pricer if we'll set the price directly.
  unless options.key?(:price) || options.key?('price')
    self.money_price = variant.price_for(pricing_options)
  end
end

#pre_tax_amountBigDecimal

Note:

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

Returns the amount of this line item before included tax.

Returns:

  • (BigDecimal)

    the amount of this line item before included tax



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

def pre_tax_amount
  discounted_amount - included_tax_total
end

#pricing_optionsObject



121
122
123
# File 'app/models/spree/line_item.rb', line 121

def pricing_options
  Spree::Config.pricing_options_class.from_line_item(self)
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



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

delegate :product, to: :variant

#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



95
96
97
# File 'app/models/spree/line_item.rb', line 95

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