Class: Spree::LineItem

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DisplayMoney

money_methods

Instance Attribute Details

#target_shipmentObject

Returns the value of attribute target_shipment.



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

def target_shipment
  @target_shipment
end

Instance Method Details

#amountObject Also known as: subtotal



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

def amount
  price * quantity
end

#any_shipped?Boolean

returns true if any of the inventory units are shipped

Returns:



147
148
149
# File 'app/models/spree/line_item.rb', line 147

def any_shipped?
  inventory_units.any?(&:shipped?)
end

#compare_at_amountObject



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

def compare_at_amount
  (variant.compare_at_amount_in(currency) || 0) * quantity
end

#copy_priceObject



66
67
68
69
70
71
72
# File 'app/models/spree/line_item.rb', line 66

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

#copy_tax_categoryObject



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

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

#discounted_priceObject



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

def discounted_price
  return price if quantity.zero?

  price - (promo_total.abs / quantity)
end

#final_amountObject Also known as: total



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

def final_amount
  amount + adjustment_total
end

#fully_shipped?Boolean

returns true if all of the inventory units are shipped

Returns:



154
155
156
# File 'app/models/spree/line_item.rb', line 154

def fully_shipped?
  inventory_units.all?(&:shipped?)
end

#insufficient_stock?Boolean

Returns:



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

def insufficient_stock?
  !sufficient_stock?
end

#item_weightBigDecimal

Returns the weight of the line item

Returns:

  • (BigDecimal)


129
130
131
# File 'app/models/spree/line_item.rb', line 129

def item_weight
  variant.weight * quantity
end

#maximum_quantityInteger

Returns the maximum quantity that can be added to the line item

Returns:

  • (Integer)


193
194
195
# File 'app/models/spree/line_item.rb', line 193

def maximum_quantity
  @maximum_quantity ||= variant.backorderable? ? Spree::DatabaseTypeUtilities.maximum_value_for(:integer) : variant.total_on_hand
end

#options=(options = {}) ⇒ Object



179
180
181
182
183
184
185
186
187
188
# File 'app/models/spree/line_item.rb', line 179

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)

  update_price_from_modifier(currency, opts)
  assign_attributes opts
end

#shipping_costBigDecimal

Returns the shipping cost for the line item

Returns:

  • (BigDecimal)


161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'app/models/spree/line_item.rb', line 161

def shipping_cost
  shipments.sum do |shipment|
    # Skip cancelled shipments
    return BigDecimal('0') if shipment.canceled?

    # Get all inventory units in this shipment for this line item
    line_item_units = shipment.inventory_units.where(line_item_id: id).count

    # Get total inventory units in this shipment
    total_units = shipment.inventory_units.count

    # Calculate proportional shipping cost
    return BigDecimal('0') if total_units.zero? || line_item_units.zero? || shipment.cost.zero?

    shipment.cost * (line_item_units.to_d / total_units)
  end
end

#sufficient_stock?Boolean

Returns:



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

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

#tax_totalBigDecimal

returns the total tax amount

Returns:

  • (BigDecimal)


115
116
117
# File 'app/models/spree/line_item.rb', line 115

def tax_total
  included_tax_total + additional_tax_total
end

#taxable_amountObject Also known as: discounted_amount



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

def taxable_amount
  amount + taxable_adjustment_total
end

#update_priceObject



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

def update_price
  currency_price = variant.price_in(order.currency)

  self.price = currency_price.price_including_vat_for(tax_zone: tax_zone) if currency_price.present?
end

#with_digital_assets?Boolean

Returns:



197
198
199
# File 'app/models/spree/line_item.rb', line 197

def with_digital_assets?
  variant.with_digital_assets?
end