Class: MerchantSidekick::ShoppingCart::LineItem

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/merchant_sidekick/shopping_cart/line_item.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



13
14
15
# File 'lib/merchant_sidekick/shopping_cart/line_item.rb', line 13

def options
  @options
end

Instance Method Details

#descriptionObject

Returns a localized description text e.g. “12 months subscription for $74 ($5.95 per month)”



146
147
148
# File 'lib/merchant_sidekick/shopping_cart/line_item.rb', line 146

def description
  self[:description]
end

#item_numberObject

returns or sets the item number based on the product’s SKU, number, or id



112
113
114
# File 'lib/merchant_sidekick/shopping_cart/line_item.rb', line 112

def item_number
  self[:item_number]
end

#nameObject

Returns a product name and sets the title attribute



106
107
108
# File 'lib/merchant_sidekick/shopping_cart/line_item.rb', line 106

def name
  self[:name]
end

#price_is_gross?Boolean

overwrites from price_is_gross? from acts_as_sellable

Returns:

  • (Boolean)


101
102
103
# File 'lib/merchant_sidekick/shopping_cart/line_item.rb', line 101

def price_is_gross?
  !price_is_net?
end

#price_is_net?Boolean

overwrites from price_is_net? from product

Returns:

  • (Boolean)


93
94
95
96
97
98
# File 'lib/merchant_sidekick/shopping_cart/line_item.rb', line 93

def price_is_net?
  if self.product
    return self.product.send(:price_is_net?) if self.product.respond_to?(:price_is_net?)
  end
  true
end

#price_per_pieceObject

Price per piece Example:

12 bottles (pieces) of a unit price of $45.00 per box (unit price)
is a per piece price of $3.75


140
141
142
# File 'lib/merchant_sidekick/shopping_cart/line_item.rb', line 140

def price_per_piece
  self.unit_price / (self.pieces || 1) if self.unit_price
end

#product_with_price=(a_product) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/merchant_sidekick/shopping_cart/line_item.rb', line 29

def product_with_price=(a_product)
  if a_product && (a_product.respond_to?(:price) || a_product.respond_to?(:copy_price))
    self[:taxable] = if a_product.respond_to?(:taxable?)
      a_product.send(:taxable?)
    elsif a_product.respond_to?(:taxable)
      a_product.send(:taxable)
    else
      false
    end
    self[:unit] = a_product.respond_to?(:unit) ? a_product.unit.to_s : 'piece'
    self[:pieces] = a_product.respond_to?(:pieces) ? a_product.pieces : 1,

    # name from product copy_name method, name or title column
    self[:name] = if a_product.respond_to?(:copy_name)
      a_product.send(:copy_name, self.options)
    elsif a_product.respond_to?(:title)
      a_product.send(:title)
    elsif a_product.respond_to?(:name)
      a_product.send(:name)
    else
      'No name'
    end

    # item_number from product copy_iten_number method, sku or number column
    self[:item_number] = if a_product.respond_to?(:copy_item_number)
      a_product.send(:copy_item_number, self.options)
    elsif a_product.respond_to?(:sku)
      a_product.send(:sku)
    elsif a_product.respond_to?(:number)
      a_product.send(:number)
    else
      a_product.id unless a_product.new_record?
    end

    # description from product copy_description or description column
    self[:description] = if a_product.respond_to?(:copy_description)
      a_product.send(:copy_description, self.options)
    elsif a_product.respond_to?(:description)
      a_product.send(:description)
    else
      'No description'
    end

    # unit price
    product_unit_price = if a_product.respond_to?(:copy_price)
      a_product.send(:copy_price, self.options)
    else
      a_product.send(:price)
    end
    product_unit_price = ::Money.new(1, self.currency || 'USD') + product_unit_price - ::Money.new(1, self.currency || 'USD')
    self.unit_price = product_unit_price
  end
  self.product_without_price = a_product
end

#taxable?Boolean Also known as: is_taxable?

overwrites from taxable? from acts_as_sellable Important! don’t change this to is_taxable?, rather alias it!

Returns:

  • (Boolean)


87
88
89
# File 'lib/merchant_sidekick/shopping_cart/line_item.rb', line 87

def taxable?
  self.taxable
end

#total_amountObject Also known as: price

total price = unit_price * quantity unit_price is defined as money!



130
131
132
# File 'lib/merchant_sidekick/shopping_cart/line_item.rb', line 130

def total_amount
  self.unit_price * (self.quantity || 1)
end

#unitObject

Make the unit a symbol getter



118
119
120
# File 'lib/merchant_sidekick/shopping_cart/line_item.rb', line 118

def unit
  self[:unit].to_s.empty? ? :piece : self[:unit].to_sym
end

#unit=(a_unit) ⇒ Object

Make the unit a symbol setter



124
125
126
# File 'lib/merchant_sidekick/shopping_cart/line_item.rb', line 124

def unit=(a_unit)
  self[:unit] = a_unit.to_s
end