Class: Caboose::CartController

Inherits:
ApplicationController show all
Defined in:
app/controllers/caboose/cart_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#admin_add, #admin_bulk_add, #admin_bulk_delete, #admin_bulk_update, #admin_delete, #admin_edit, #admin_index, #admin_json, #admin_json_single, #admin_update, #before_action, #before_before_action, #hashify_query_string, #init_cart, #logged_in?, #logged_in_user, #login_user, #logout_user, #parse_url_params, #reject_param, #user_is_allowed, #user_is_allowed_to, #validate_cookie, #validate_token, #var, #verify_logged_in

Instance Method Details

#addObject

POST /cart



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/controllers/caboose/cart_controller.rb', line 41

def add
  variant_id = params[:variant_id]
  qty = params[:quantity] ? params[:quantity].to_i : 1
  
  if @order.line_items.exists?(:variant_id => variant_id)
    li = @order.line_items.find_by_variant_id(variant_id)
    li.quantity += qty
  else
    li = LineItem.new(
      :order_id   => @order.id,
      :variant_id => variant_id,
      :quantity   => qty,
      :status     => 'pending'
    )
  end       
  render :json => { 
    :success => li.save, 
    :errors => li.errors.full_messages,
    :item_count => @order.item_count 
  }
end

#add_gift_cardObject

POST /cart/gift-cards



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/controllers/caboose/cart_controller.rb', line 80

def add_gift_card      
  resp = StdClass.new
  code = params[:code].strip
  gc = GiftCard.where("lower(code) = ?", code.downcase).first
  
  if gc.nil? then resp.error = "Invalid gift card code."                    
  elsif gc.status != GiftCard::STATUS_ACTIVE                                then resp.error = "That gift card is not active."
  elsif gc.date_available && DateTime.now.utc < self.date_available         then resp.error = "That gift card is not active yet."         
  elsif gc.date_expires && DateTime.now.utc > self.date_expires             then resp.error = "That gift card is expired."
  elsif gc.card_type == GiftCard::CARD_TYPE_AMOUNT && gc.balance <= 0       then resp.error = "That gift card has a zero balance." 
  elsif gc.min_order_total && @order.total < gc.min_order_total             then resp.error = "Your order must be at least $#{sprintf('%.2f',gc.min_order_total)} to use this gift card." 
  elsif Discount.where(:order_id => @order.id, :gift_card_id => gc.id).exists? then resp.error = "That gift card has already been applied to this order."
  else
    # Determine how much the discount will be
    d = Discount.new(:order_id => @order.id, :gift_card_id => gc.id, :amount => 0.0)
    case gc.card_type
      when GiftCard::CARD_TYPE_AMOUNT      then d.amount = (@order.total > gc.balance ? gc.balance : @order.total)
      when GiftCard::CARD_TYPE_PERCENTAGE  then d.amount = @order.subtotal * gc.total
      when GiftCard::CARD_TYPE_NO_SHIPPING then d.amount = @order.shipping
      when GiftCard::CARD_TYPE_NO_TAX      then d.amount = @order.tax
    end
    d.save
    @order.calculate
    resp.success = true
    resp.order_total = @order.total
  end
  render :json => resp
end

#indexObject

GET /cart



5
6
# File 'app/controllers/caboose/cart_controller.rb', line 5

def index
end

#item_countObject

GET /cart/item-count



36
37
38
# File 'app/controllers/caboose/cart_controller.rb', line 36

def item_count
  render :json => { :item_count => @order.item_count }
end

#listObject

GET /cart/items



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/caboose/cart_controller.rb', line 9

def list
  render :json => @order.as_json(
    :include => [        
      { 
        :line_items => { 
          :include => { 
            :variant => { 
              :include => [
                { :product_images => { :methods => :urls }},
                { :product => { :include => { :product_images => { :methods => :urls }}}}
              ],
              :methods => :title
            }
          }
        }
      },
      { :order_packages => { :include => [:shipping_package, :shipping_method] }},
      :customer,
      :shipping_address,
      :billing_address,
      :order_transactions,
      { :discounts => { :include => :gift_card }}
    ]        
  )
end

#removeObject

DELETE /cart/:line_item_id



74
75
76
77
# File 'app/controllers/caboose/cart_controller.rb', line 74

def remove
  li = LineItem.find(params[:line_item_id]).destroy
  render :json => { :success => true, :item_count => @order.line_items.count }
end

#remove_discountObject

DELETE /cart/discounts/:discount_id



110
111
112
113
114
# File 'app/controllers/caboose/cart_controller.rb', line 110

def remove_discount
  Discount.find(params[:discount_id]).destroy
  @order.calculate
  render :json => { :success => true }
end

#updateObject

PUT /cart/:line_item_id



64
65
66
67
68
69
70
71
# File 'app/controllers/caboose/cart_controller.rb', line 64

def update
  li = LineItem.find(params[:line_item_id])
  li.quantity = params[:quantity].to_i
  li.save
  li.destroy if li.quantity == 0
  @order.calculate_subtotal
  render :json => { :success => true }
end