Class: Spree::LineItem
- Extended by:
- DisplayMoney
- Defined in:
- app/models/spree/line_item.rb
Overview
Variants placed in the Order at a particular price.
‘Spree::LineItem` is an ActiveRecord model which records which `Spree::Variant` a customer has chosen to place in their order. It also acts as the permenent record of the customer’s order by recording relevant price, taxation, and inventory concerns. Line items can also have adjustments placed on them as part of the promotion system.
Defined Under Namespace
Classes: CurrencyMismatch
Instance Attribute Summary collapse
-
#target_shipment ⇒ Object
Returns the value of attribute target_shipment.
Instance Method Summary collapse
-
#amount ⇒ BigDecimal
(also: #subtotal)
The amount of this line item, which is the line item’s price multiplied by its quantity.
- #currency=(_currency) ⇒ Object
-
#discounted_amount ⇒ BigDecimal
The amount of this line item, taking into consideration line item promotions.
-
#insufficient_stock? ⇒ Boolean
True when it is not possible to supply the required quantity of stock of this line item’s variant.
-
#money_price=(money) ⇒ Object
Sets price from a ‘Spree::Money` object.
-
#options=(options = {}) ⇒ Object
Sets options on the line item and updates the price.
- #pricing_options ⇒ Object
-
#sufficient_stock? ⇒ Boolean
True when it is possible to supply the required quantity of stock of this line item’s variant.
-
#total ⇒ BigDecimal
(also: #final_amount)
The amount of this line item, taking into consideration all its adjustments.
-
#total_before_tax ⇒ BigDecimal
The amount of this item, taking into consideration all non-tax adjustments.
-
#total_excluding_vat ⇒ BigDecimal
(also: #pre_tax_amount)
The amount of this line item before VAT tax.
Methods included from DisplayMoney
Methods inherited from Base
display_includes, #initialize_preference_defaults, page, preference
Methods included from Preferences::Preferable
#admin_form_preference_names, #default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference
Instance Attribute Details
#target_shipment ⇒ Object
Returns the value of attribute target_shipment.
45 46 47 |
# File 'app/models/spree/line_item.rb', line 45 def target_shipment @target_shipment end |
Instance Method Details
#amount ⇒ BigDecimal Also known as: subtotal
Returns the amount of this line item, which is the line item’s price multiplied by its quantity.
52 53 54 |
# File 'app/models/spree/line_item.rb', line 52 def amount price * quantity end |
#currency=(_currency) ⇒ Object
156 157 158 159 160 |
# File 'app/models/spree/line_item.rb', line 156 def currency=(_currency) Spree::Deprecation.warn 'Spree::LineItem#currency= is deprecated ' \ 'and will take no effect.', caller end |
#discounted_amount ⇒ BigDecimal
Returns the amount of this line item, taking into consideration line item promotions.
59 60 61 |
# File 'app/models/spree/line_item.rb', line 59 def discounted_amount amount + promo_total end |
#insufficient_stock? ⇒ Boolean
Returns true when it is not possible to supply the required quantity of stock of this line item’s variant.
129 130 131 |
# File 'app/models/spree/line_item.rb', line 129 def insufficient_stock? !sufficient_stock? end |
#money_price=(money) ⇒ Object
Sets price from a ‘Spree::Money` object
111 112 113 114 115 116 117 118 119 |
# File 'app/models/spree/line_item.rb', line 111 def money_price=(money) if !money self.price = nil elsif money.currency.iso_code != currency raise CurrencyMismatch, "Line item price currency must match order currency!" else self.price = money.to_d end end |
#options=(options = {}) ⇒ Object
Sets options on the line item and updates the price.
The options can be arbitrary attributes on the LineItem.
138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'app/models/spree/line_item.rb', line 138 def ( = {}) return unless .present? assign_attributes # When price is part of the options we are not going to fetch # it from the variant. Please note that this always allows to set # a price for this line item, even if there is no existing price # for the associated line item in the order currency. unless .key?(:price) || .key?('price') self.money_price = variant.price_for() end end |
#pricing_options ⇒ Object
152 153 154 |
# File 'app/models/spree/line_item.rb', line 152 def Spree::Config..from_line_item(self) end |
#sufficient_stock? ⇒ Boolean
Returns true when it is possible to supply the required quantity of stock of this line item’s variant.
123 124 125 |
# File 'app/models/spree/line_item.rb', line 123 def sufficient_stock? Stock::Quantifier.new(variant).can_supply? quantity end |
#total ⇒ BigDecimal Also known as: final_amount
Returns the amount of this line item, taking into consideration all its adjustments.
66 67 68 |
# File 'app/models/spree/line_item.rb', line 66 def total amount + adjustment_total end |
#total_before_tax ⇒ BigDecimal
Returns the amount of this item, taking into consideration all non-tax adjustments.
74 75 76 |
# File 'app/models/spree/line_item.rb', line 74 def total_before_tax amount + adjustments.select { |a| !a.tax? && a.eligible? }.sum(&:amount) end |
#total_excluding_vat ⇒ BigDecimal Also known as: pre_tax_amount
just like ‘amount`, this does not include any additional tax
Returns the amount of this line item before VAT tax.
80 81 82 |
# File 'app/models/spree/line_item.rb', line 80 def total_excluding_vat total_before_tax - included_tax_total end |