Class: Spree::LineItem
- Inherits:
-
Object
- Object
- Spree::LineItem
- Extended by:
- DisplayMoney
- Includes:
- Metadata, Metafields, Webhooks::HasWebhooks
- Defined in:
- app/models/spree/line_item.rb
Instance Attribute Summary collapse
-
#target_shipment ⇒ Object
Returns the value of attribute target_shipment.
Instance Method Summary collapse
- #amount ⇒ Object (also: #subtotal)
-
#any_shipped? ⇒ Boolean
returns true if any of the inventory units are shipped.
- #compare_at_amount ⇒ Object
- #copy_price ⇒ Object
- #copy_tax_category ⇒ Object
- #discounted_price ⇒ Object
- #final_amount ⇒ Object (also: #total)
-
#fully_shipped? ⇒ Boolean
returns true if all of the inventory units are shipped.
- #insufficient_stock? ⇒ Boolean
-
#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
-
#shipping_cost ⇒ BigDecimal
Returns the shipping cost for the line item.
- #sufficient_stock? ⇒ Boolean
-
#tax_total ⇒ BigDecimal
returns the total tax amount.
- #taxable_amount ⇒ Object (also: #discounted_amount)
- #update_price ⇒ Object
- #with_digital_assets? ⇒ Boolean
Methods included from DisplayMoney
Instance Attribute Details
#target_shipment ⇒ Object
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
#amount ⇒ Object 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
147 148 149 |
# File 'app/models/spree/line_item.rb', line 147 def any_shipped? inventory_units.any?(&:shipped?) end |
#compare_at_amount ⇒ Object
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_price ⇒ Object
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_category ⇒ Object
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_price ⇒ Object
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_amount ⇒ Object 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
154 155 156 |
# File 'app/models/spree/line_item.rb', line 154 def fully_shipped? inventory_units.all?(&:shipped?) end |
#insufficient_stock? ⇒ Boolean
140 141 142 |
# File 'app/models/spree/line_item.rb', line 140 def insufficient_stock? !sufficient_stock? end |
#item_weight ⇒ BigDecimal
Returns the weight of the line item
129 130 131 |
# File 'app/models/spree/line_item.rb', line 129 def item_weight variant.weight * quantity end |
#maximum_quantity ⇒ Integer
Returns the maximum quantity that can be added to the line item
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 ( = {}) 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 |
#shipping_cost ⇒ BigDecimal
Returns the shipping cost for the line item
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
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_total ⇒ BigDecimal
returns the total tax amount
115 116 117 |
# File 'app/models/spree/line_item.rb', line 115 def tax_total included_tax_total + additional_tax_total end |
#taxable_amount ⇒ Object 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_price ⇒ Object
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
197 198 199 |
# File 'app/models/spree/line_item.rb', line 197 def with_digital_assets? variant.with_digital_assets? end |