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

page

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.



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

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.



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

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.



45
46
47
48
49
50
51
# File 'app/models/spree/line_item.rb', line 45

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.



55
56
57
58
59
# File 'app/models/spree/line_item.rb', line 55

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.



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

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.



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

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.



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

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



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

def insufficient_stock?
  !sufficient_stock?
end

#invalid_quantity_checkObject

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



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

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



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

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



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'app/models/spree/line_item.rb', line 135

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



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

def product
  variant.product
end

#single_moneySpree::Money Also known as: single_display_amount

Returns the price of this line item.

Returns:



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

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



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

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

#variantSpree::Variant?

Note:

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

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

Returns:

  • (Spree::Variant, nil)

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



127
128
129
# File 'app/models/spree/line_item.rb', line 127

def variant
  Spree::Variant.unscoped { super }
end