Class: Spree::LineItem

Inherits:
Object
  • Object
show all
Extended by:
DisplayMoney
Includes:
Metadata, Metafields
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

#amountBigDecimal Also known as: subtotal

Returns the amount (price * quantity) of the line item

Returns:

  • (BigDecimal)


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

def amount
  price * quantity
end

#any_shipped?Boolean

returns true if any of the inventory units are shipped

Returns:



167
168
169
# File 'app/models/spree/line_item.rb', line 167

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

#compare_at_amountBigDecimal

Returns the compare at amount (compare at price * quantity) of the line item

Returns:

  • (BigDecimal)


110
111
112
# File 'app/models/spree/line_item.rb', line 110

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



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

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

#discounted_priceObject



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

def discounted_price
  return price if quantity.zero?

  price - (promo_total.abs / quantity)
end

#final_amountBigDecimal Also known as: total

Returns the final amount of the line item

Returns:

  • (BigDecimal)


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

def final_amount
  amount + adjustment_total
end

#fully_shipped?Boolean

returns true if all of the inventory units are shipped

Returns:



174
175
176
# File 'app/models/spree/line_item.rb', line 174

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

#insufficient_stock?Boolean

Returns true if the line item has insufficient stock

Returns:



160
161
162
# File 'app/models/spree/line_item.rb', line 160

def insufficient_stock?
  !sufficient_stock?
end

#item_weightBigDecimal

Returns the weight of the line item

Returns:

  • (BigDecimal)


143
144
145
# File 'app/models/spree/line_item.rb', line 143

def item_weight
  variant.weight * quantity
end

#maximum_quantityInteger

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

Returns:

  • (Integer)


219
220
221
# File 'app/models/spree/line_item.rb', line 219

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

#options=(options = {}) ⇒ Object



205
206
207
208
209
210
211
212
213
214
# File 'app/models/spree/line_item.rb', line 205

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

#recalculate_pricevoid

This method returns an undefined value.

Recalculates and persists the price based on the current quantity and pricing context This is used for volume-based pricing and other price list rules



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'app/models/spree/line_item.rb', line 233

def recalculate_price
  context = Spree::Pricing::Context.from_order(variant, order, quantity: quantity)
  currency_price = variant.price_for(context)

  return unless currency_price.present?

  new_price = currency_price.price_including_vat_for(tax_zone: tax_zone)

  return unless new_price.present?

  new_price_list_id = currency_price.price_list_id

  # Only update if price or price list changed
  if new_price != price || new_price_list_id != price_list_id
    update_columns(price: new_price, price_list_id: new_price_list_id, updated_at: Time.current)
  end
end

#shipping_costBigDecimal

Returns the shipping cost for the line item

Returns:

  • (BigDecimal)


181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'app/models/spree/line_item.rb', line 181

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

    # Skip shipments with no cost/zero cost
    return BigDecimal('0') if shipment.cost.zero?

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

    # Calculate proportional shipping cost
    return BigDecimal('0') if total_units.empty?

    # Get all inventory units in this shipment for this line item
    line_item_units = shipment.inventory_units.find_all { |unit| unit.line_item_id == id }.count

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

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

#sufficient_stock?Boolean

Returns true if the line item has sufficient stock

Returns:



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

def sufficient_stock?
  can_supply? quantity
end

#tax_totalBigDecimal

returns the total tax amount

Returns:

  • (BigDecimal)


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

def tax_total
  included_tax_total + additional_tax_total
end

#taxable_amountBigDecimal Also known as: discounted_amount

Returns the taxable amount (amount + taxable adjustment total) of the line item

Returns:

  • (BigDecimal)


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

def taxable_amount
  amount + taxable_adjustment_total
end

#update_priceObject



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

def update_price
  context = Spree::Pricing::Context.from_order(variant, order, quantity: quantity)
  currency_price = variant.price_for(context)

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

#with_digital_assets?Boolean

Returns true if the line item variant has digital assets

Returns:



226
227
228
# File 'app/models/spree/line_item.rb', line 226

def with_digital_assets?
  variant.with_digital_assets?
end