Class: Spree::LineItem
- Inherits:
-
Object
- Object
- Spree::LineItem
- Extended by:
- DisplayMoney
- Includes:
- Metadata, Metafields
- Defined in:
- app/models/spree/line_item.rb
Constant Summary collapse
- DB_INTEGER_MAX =
(2**31) - 1
Instance Attribute Summary collapse
-
#target_shipment ⇒ Object
Returns the value of attribute target_shipment.
Instance Method Summary collapse
-
#amount ⇒ BigDecimal
(also: #subtotal)
Returns the amount (price * quantity) of the line item.
-
#any_shipped? ⇒ Boolean
returns true if any of the inventory units are shipped.
-
#compare_at_amount ⇒ BigDecimal
Returns the compare at amount (compare at price * quantity) of the line item.
- #copy_price ⇒ Object
- #copy_tax_category ⇒ Object
- #discounted_price ⇒ Object
-
#final_amount ⇒ BigDecimal
(also: #total)
Returns the final amount of the line item.
-
#fully_shipped? ⇒ Boolean
returns true if all of the inventory units are shipped.
-
#insufficient_stock? ⇒ Boolean
Returns true if the line item has insufficient stock.
-
#item_weight ⇒ BigDecimal
Returns the weight of the line item.
-
#maximum_quantity ⇒ Integer
Returns the maximum quantity that can be added to the line item.
- #options=(options = {}) ⇒ Object
-
#recalculate_price ⇒ void
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.
-
#shipping_cost ⇒ BigDecimal
Returns the shipping cost for the line item.
-
#sufficient_stock? ⇒ Boolean
Returns true if the line item has sufficient stock.
-
#tax_total ⇒ BigDecimal
returns the total tax amount.
-
#taxable_amount ⇒ BigDecimal
(also: #discounted_amount)
Returns the taxable amount (amount + taxable adjustment total) of the line item.
-
#thumbnail ⇒ Spree::Asset?
Returns the thumbnail image for this line item Prefers variant primary media, falls back to product primary media.
- #update_price ⇒ Object
-
#with_digital_assets? ⇒ Boolean
Returns true if the line item variant has digital assets.
Methods included from DisplayMoney
Methods included from Metadata
#metadata, #metadata=, #public_metadata=
Instance Attribute Details
#target_shipment ⇒ Object
Returns the value of attribute target_shipment.
69 70 71 |
# File 'app/models/spree/line_item.rb', line 69 def target_shipment @target_shipment end |
Instance Method Details
#amount ⇒ BigDecimal Also known as: subtotal
Returns the amount (price * quantity) of the line item
119 120 121 |
# File 'app/models/spree/line_item.rb', line 119 def amount price * quantity end |
#any_shipped? ⇒ Boolean
returns true if any of the inventory units are shipped
187 188 189 |
# File 'app/models/spree/line_item.rb', line 187 def any_shipped? inventory_units.any?(&:shipped?) end |
#compare_at_amount ⇒ BigDecimal
Returns the compare at amount (compare at price * quantity) of the line item
126 127 128 |
# File 'app/models/spree/line_item.rb', line 126 def compare_at_amount (variant.compare_at_amount_in(currency) || 0) * quantity end |
#copy_price ⇒ Object
78 79 80 81 82 83 84 |
# File 'app/models/spree/line_item.rb', line 78 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_category ⇒ Object
94 95 96 |
# File 'app/models/spree/line_item.rb', line 94 def copy_tax_category self.tax_category = variant.tax_category if variant end |
#discounted_price ⇒ Object
110 111 112 113 114 |
# File 'app/models/spree/line_item.rb', line 110 def discounted_price return price if quantity.zero? price - (promo_total.abs / quantity) end |
#final_amount ⇒ BigDecimal Also known as: total
Returns the final amount of the line item
152 153 154 |
# File 'app/models/spree/line_item.rb', line 152 def final_amount amount + adjustment_total end |
#fully_shipped? ⇒ Boolean
returns true if all of the inventory units are shipped
194 195 196 |
# File 'app/models/spree/line_item.rb', line 194 def fully_shipped? inventory_units.all?(&:shipped?) end |
#insufficient_stock? ⇒ Boolean
Returns true if the line item has insufficient stock
180 181 182 |
# File 'app/models/spree/line_item.rb', line 180 def insufficient_stock? !sufficient_stock? end |
#item_weight ⇒ BigDecimal
Returns the weight of the line item
159 160 161 |
# File 'app/models/spree/line_item.rb', line 159 def item_weight variant.weight * quantity end |
#maximum_quantity ⇒ Integer
Returns the maximum quantity that can be added to the line item
239 240 241 |
# File 'app/models/spree/line_item.rb', line 239 def maximum_quantity @maximum_quantity ||= variant.backorderable? ? DB_INTEGER_MAX : variant.total_on_hand end |
#options=(options = {}) ⇒ Object
225 226 227 228 229 230 231 232 233 234 |
# File 'app/models/spree/line_item.rb', line 225 def ( = {}) return unless .present? opts = .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_price ⇒ void
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
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'app/models/spree/line_item.rb', line 253 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_cost ⇒ BigDecimal
Returns the shipping cost for the line item
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'app/models/spree/line_item.rb', line 201 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
The order’s own active stock reservations are excluded from the availability check — a customer’s own checkout hold must not make their own line item look out of stock.
173 174 175 |
# File 'app/models/spree/line_item.rb', line 173 def sufficient_stock? Spree::Stock::Quantifier.new(variant, excluded_order: order).can_supply?(quantity) end |
#tax_total ⇒ BigDecimal
returns the total tax amount
142 143 144 |
# File 'app/models/spree/line_item.rb', line 142 def tax_total included_tax_total + additional_tax_total end |
#taxable_amount ⇒ BigDecimal Also known as: discounted_amount
Returns the taxable amount (amount + taxable adjustment total) of the line item
135 136 137 |
# File 'app/models/spree/line_item.rb', line 135 def taxable_amount amount + taxable_adjustment_total end |
#thumbnail ⇒ Spree::Asset?
Returns the thumbnail image for this line item Prefers variant primary media, falls back to product primary media
61 62 63 |
# File 'app/models/spree/line_item.rb', line 61 def thumbnail variant.primary_media || product.primary_media end |
#update_price ⇒ Object
86 87 88 89 90 91 92 |
# File 'app/models/spree/line_item.rb', line 86 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
246 247 248 |
# File 'app/models/spree/line_item.rb', line 246 def with_digital_assets? variant.with_digital_assets? end |