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.
68 69 70 |
# File 'app/models/spree/line_item.rb', line 68 def target_shipment @target_shipment end |
Instance Method Details
#amount ⇒ BigDecimal Also known as: subtotal
Returns the amount (price * quantity) of the line item
118 119 120 |
# File 'app/models/spree/line_item.rb', line 118 def amount price * quantity end |
#any_shipped? ⇒ Boolean
returns true if any of the inventory units are shipped
182 183 184 |
# File 'app/models/spree/line_item.rb', line 182 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
125 126 127 |
# File 'app/models/spree/line_item.rb', line 125 def compare_at_amount (variant.compare_at_amount_in(currency) || 0) * quantity end |
#copy_price ⇒ Object
77 78 79 80 81 82 83 |
# File 'app/models/spree/line_item.rb', line 77 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
93 94 95 |
# File 'app/models/spree/line_item.rb', line 93 def copy_tax_category self.tax_category = variant.tax_category if variant end |
#discounted_price ⇒ Object
109 110 111 112 113 |
# File 'app/models/spree/line_item.rb', line 109 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
151 152 153 |
# File 'app/models/spree/line_item.rb', line 151 def final_amount amount + adjustment_total end |
#fully_shipped? ⇒ Boolean
returns true if all of the inventory units are shipped
189 190 191 |
# File 'app/models/spree/line_item.rb', line 189 def fully_shipped? inventory_units.all?(&:shipped?) end |
#insufficient_stock? ⇒ Boolean
Returns true if the line item has insufficient stock
175 176 177 |
# File 'app/models/spree/line_item.rb', line 175 def insufficient_stock? !sufficient_stock? end |
#item_weight ⇒ BigDecimal
Returns the weight of the line item
158 159 160 |
# File 'app/models/spree/line_item.rb', line 158 def item_weight variant.weight * quantity end |
#maximum_quantity ⇒ Integer
Returns the maximum quantity that can be added to the line item
234 235 236 |
# File 'app/models/spree/line_item.rb', line 234 def maximum_quantity @maximum_quantity ||= variant.backorderable? ? DB_INTEGER_MAX : variant.total_on_hand end |
#options=(options = {}) ⇒ Object
220 221 222 223 224 225 226 227 228 229 |
# File 'app/models/spree/line_item.rb', line 220 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
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'app/models/spree/line_item.rb', line 248 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
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'app/models/spree/line_item.rb', line 196 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
168 169 170 |
# File 'app/models/spree/line_item.rb', line 168 def sufficient_stock? can_supply? quantity end |
#tax_total ⇒ BigDecimal
returns the total tax amount
141 142 143 |
# File 'app/models/spree/line_item.rb', line 141 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
134 135 136 |
# File 'app/models/spree/line_item.rb', line 134 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
60 61 62 |
# File 'app/models/spree/line_item.rb', line 60 def thumbnail variant.primary_media || product.primary_media end |
#update_price ⇒ Object
85 86 87 88 89 90 91 |
# File 'app/models/spree/line_item.rb', line 85 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
241 242 243 |
# File 'app/models/spree/line_item.rb', line 241 def with_digital_assets? variant.with_digital_assets? end |