Class: Cart

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/cart.rb

Overview

Schema Information

Table name: carts

id          :integer(4)      not null, primary key
user_id     :integer(4)
created_at  :datetime
updated_at  :datetime
customer_id :integer(4)

Instance Method Summary (collapse)

Instance Method Details

- (order) add_items_to_checkout(order)

Call this method when you are checking out with the current cart items

> these will now be order.order_items

> the order can only add items if it is 'in_progress'

Parameters:

  • order (Order)

    to insert the shopping cart variants into

Returns:

  • (order)

    return order because teh order returned has a diffent quantity



128
129
130
131
132
133
134
135
136
137
# File 'app/models/cart.rb', line 128

def add_items_to_checkout(order)
  if order.in_progress?
    order.order_items.size.times do
      item = order.order_items.pop
      item.destroy
    end
    items_to_add(order, shopping_cart_items)
  end
  order
end

- (CartItem) add_variant(variant_id, customer, qty = 1, cart_item_type_id = ItemType::SHOPPING_CART_ID, admin_purchase = false)

Call this method when you want to add an item to the shopping cart

Parameters:

  • variant (Integer, #read)

    id to add to the cart

  • user (User, #read)

    that is adding something to the cart

  • ItemType (Integer, #optional)

    id that is being added to the cart

Returns:

  • (CartItem)

    return the cart item that is added to the cart



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/models/cart.rb', line 145

def add_variant(variant_id, customer, qty = 1, cart_item_type_id = ItemType::SHOPPING_CART_ID, admin_purchase = false)
  items = shopping_cart_items.find_all_by_variant_id(variant_id)
  variant = Variant.find(variant_id)
  quantity_to_purchase = (variant.quantity_purchaseable(admin_purchase) < qty.to_i) ? variant.quantity_purchaseable(admin_purchase) : qty.to_i # if we have less than desired instock

  if admin_purchase && (quantity_to_purchase > 0)
    cart_item = add_cart_items(items, quantity_to_purchase, customer, cart_item_type_id, variant_id)
  elsif variant.sold_out?
    cart_item = saved_cart_items.create(:variant_id   => variant_id,
                                  :user         => customer,
                                  :item_type_id => ItemType::SAVE_FOR_LATER_ID,
                                  :quantity     => qty#,#:price      => variant.price
                                  ) if items.size < 1
  else
    cart_item = add_cart_items(items, quantity_to_purchase, customer, cart_item_type_id, variant_id)
  end
  cart_item
end

- (Object) mark_items_purchased(order)

Call this method when you want to mark the items in the order as purchased

The CartItem will not delete.  Instead the item_type changes to purchased

Parameters:



189
190
191
192
# File 'app/models/cart.rb', line 189

def mark_items_purchased(order)
  CartItem.update_all("item_type_id = #{ItemType::PURCHASED_ID}",
                      "id IN (#{(self.cart_item_ids + self.shopping_cart_item_ids).uniq.join(',')}) AND variant_id IN (#{order.variant_ids.join(',')})") if !order.variant_ids.empty?
end

- (CartItem) remove_variant(variant_id)

Call this method when you want to remove an item from the shopping cart

The CartItem will not delete.  Instead it is just inactivated

Parameters:

  • variant (Integer, #read)

    id to add to the cart

Returns:

  • (CartItem)

    return the cart item that is added to the cart



170
171
172
173
# File 'app/models/cart.rb', line 170

def remove_variant(variant_id)
  citems = self.cart_items.each {|ci| ci.inactivate! if variant_id.to_i == ci.variant_id }
  return citems
end

- (Object) save_user(u)

Call this method when you want to associate the cart with a user

Parameters:



178
179
180
181
182
183
# File 'app/models/cart.rb', line 178

def save_user(u)  # u is user object or nil
  if u && self.user_id != u.id
    self.user_id = u.id
    self.save
  end
end

- (Float) sub_total

Adds all the item prices (not including taxes) that are currently in the shopping cart

Parameters:

  • (none)

Returns:

  • (Float)

    This is a float in decimal form and represents the price of all the items in the cart



118
119
120
# File 'app/models/cart.rb', line 118

def sub_total
  shopping_cart_items.inject(0) {|sum, item| item.total + sum} #.includes(:variant)
end