Class: Locomotive::Ecommerce::Cart

Inherits:
Object
  • Object
show all
Includes:
EcommerceHelper, Mongoid::Document
Defined in:
app/models/locomotive/ecommerce/cart.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EcommerceHelper

#as_currency, #current_user

Methods included from InventoryInterface

included

Methods included from EcommerceUrlHelper

#add_to_cart_path, #cart_path, #cart_update_path, #checkout_index_path, #checkout_path, #checkout_update_path, #confirm_order_path, #post_checkout_path, #purchases_path, #remove_from_cart_path

Methods included from EcommerceCartHelper

#current_user_cart

Class Method Details

.find_or_create(id, session) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/models/locomotive/ecommerce/cart.rb', line 107

def self.find_or_create(id, session)
  cart_id = session[:cart_id]
  if cart_id != nil
    cart = where(:_id => cart_id).first

    if cart == nil
      session[:cart_id] = nil
      return find_or_create(id, session)
    end

    return cart if id == nil

    session[:cart_id] = nil
    user_cart = Cart.for_user(id)
    user_cart.merge(cart)
    cart.destroy
    return user_cart
  end

  return Cart.for_user(id) if id != nil

  cart = create
  cart.save!
  session[:cart_id] = cart.id.to_s
  return cart
end

.for_user(id) ⇒ Object

self methods



103
104
105
# File 'app/models/locomotive/ecommerce/cart.rb', line 103

def self.for_user(id)
  cart = where(:user_id => id).first || create(:user_id => id)
end

Instance Method Details

#add_extras_jsObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/locomotive/ecommerce/cart.rb', line 43

def add_extras_js
  ext = Engine.config_or_default('edit_extra')
  return nil unless ext
  site = Thread.current[:site]
  cxt = site.plugin_object_for_id('ecommerce').js3_context
  cxt['purchase_total'] = purchase_total
  cxt['orders'] = orders
  cxt['purchase'] = purchase
  js = cxt.eval(ext)

  return js
end

#add_product_by_sku(sku) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/models/locomotive/ecommerce/cart.rb', line 10

def add_product_by_sku(sku)
  already_existing_order = orders.where(:sku => sku)
  if already_existing_order.count > 0
    order = already_existing_order.first
    order.quantity += 1
  else
    order = Order.new
    order.sku = sku
    order.cart = self
  end
  order.save!
  return order
end

#estimated_taxObject



35
36
37
# File 'app/models/locomotive/ecommerce/cart.rb', line 35

def estimated_tax
  purchase_total * ((Engine.config_or_default('estimated_tax_rate').to_f/100))
end

#extrasObject



68
69
70
71
72
73
# File 'app/models/locomotive/ecommerce/cart.rb', line 68

def extras
  orders ? arr = add_extras_js : ''
  # Converting the prices to currency
  extras_hash = Hash[arr.map {|k,v| [k,as_currency(v) ]}]
  extras_hash.to_liquid
end

#extras_totalObject



56
57
58
59
60
61
62
63
64
65
66
# File 'app/models/locomotive/ecommerce/cart.rb', line 56

def extras_total
  ext = 0
  arr = add_extras_js
  # Adding all the values returned by the javascript function
  if arr
    arr.values.each do |value|
      ext += value
    end
  end
  return ext
end

#merge(cart) ⇒ Object

merge in contents of another cart



86
87
88
89
90
91
# File 'app/models/locomotive/ecommerce/cart.rb', line 86

def merge(cart)
  cart.orders.each do |order|
    (1..order.quantity).each { |c| add_product_by_sku(order.sku) }
    cart.remove_product_by_sku(order.sku)
  end
end

#purchase_totalObject



29
30
31
32
33
# File 'app/models/locomotive/ecommerce/cart.rb', line 29

def purchase_total
  total = 0
  orders.each { |order| total += order.price }
  return total
end

#remove_product_by_sku(sku) ⇒ Object



24
25
26
27
# File 'app/models/locomotive/ecommerce/cart.rb', line 24

def remove_product_by_sku(sku)
  order = orders.where(:sku => sku)
  order.destroy if order
end

#subtotal_est_taxObject



39
40
41
# File 'app/models/locomotive/ecommerce/cart.rb', line 39

def subtotal_est_tax
  purchase_total + estimated_tax + extras_total
end

#to_liquidObject



98
99
100
# File 'app/models/locomotive/ecommerce/cart.rb', line 98

def to_liquid
  CartDrop.new(self)
end

#update_from_params(params) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'app/models/locomotive/ecommerce/cart.rb', line 75

def update_from_params(params)
  to_update = Order.find(params[:order_ids])
  count = 0
  to_update.each do |order|
    order.quantity = params[:quantity_ids][count] if order.cart_id == id
    count += 1
    order.save!
  end
end

#valid_stock?Boolean

Returns:

  • (Boolean)


93
94
95
96
# File 'app/models/locomotive/ecommerce/cart.rb', line 93

def valid_stock?
  orders.each { |order| return false if order.out_of_stock? }
  return true
end