Class: Caboose::CartController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- Caboose::CartController
- Defined in:
- app/controllers/caboose/cart_controller.rb
Instance Method Summary collapse
-
#add ⇒ Object
POST /cart.
-
#add_gift_card ⇒ Object
POST /cart/gift-cards.
-
#index ⇒ Object
GET /cart.
-
#item_count ⇒ Object
GET /cart/item-count.
-
#list ⇒ Object
GET /cart/items.
-
#remove ⇒ Object
DELETE /cart/:line_item_id.
-
#remove_discount ⇒ Object
DELETE /cart/discounts/:discount_id.
-
#update ⇒ Object
PUT /cart/:line_item_id.
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, #under_construction_or_forwarding_domain?, #user_is_allowed, #user_is_allowed_to, #validate_cookie, #validate_token, #var, #verify_logged_in
Instance Method Details
#add ⇒ Object
POST /cart
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 |
# File 'app/controllers/caboose/cart_controller.rb', line 41 def add v = Variant.find(params[:variant_id]) qty = params[:quantity] ? params[:quantity].to_i : 1 if @order.line_items.exists?(:variant_id => v.id) li = @order.line_items.find_by_variant_id(v.id) li.quantity += qty li.subtotal = li.unit_price * li.quantity else unit_price = v.on_sale? ? v.sale_price : v.price li = LineItem.new( :order_id => @order.id, :variant_id => v.id, :quantity => qty, :unit_price => unit_price, :subtotal => unit_price * qty, :status => 'pending' ) end render :json => { :success => li.save, :errors => li.errors., :item_count => @order.item_count } end |
#add_gift_card ⇒ Object
POST /cart/gift-cards
104 105 106 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 |
# File 'app/controllers/caboose/cart_controller.rb', line 104 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 < gc.date_available then resp.error = "That gift card is not active yet." elsif gc.date_expires && DateTime.now.utc > gc.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 |
#index ⇒ Object
GET /cart
5 6 |
# File 'app/controllers/caboose/cart_controller.rb', line 5 def index end |
#item_count ⇒ Object
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 |
#list ⇒ Object
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 |
#remove ⇒ Object
DELETE /cart/:line_item_id
98 99 100 101 |
# File 'app/controllers/caboose/cart_controller.rb', line 98 def remove li = LineItem.find(params[:line_item_id]).destroy render :json => { :success => true, :item_count => @order.line_items.count } end |
#remove_discount ⇒ Object
DELETE /cart/discounts/:discount_id
134 135 136 137 138 |
# File 'app/controllers/caboose/cart_controller.rb', line 134 def remove_discount Discount.find(params[:discount_id]).destroy @order.calculate render :json => { :success => true } end |
#update ⇒ Object
PUT /cart/:line_item_id
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'app/controllers/caboose/cart_controller.rb', line 68 def update resp = Caboose::StdClass.new li = LineItem.find(params[:line_item_id]) save = true params.each do |name,value| case name when 'quantity' then li.quantity = value.to_i if li.quantity == 0 li.destroy else li.subtotal = li.unit_price * li.quantity li.save li.order.calculate end when 'is_gift' then li.is_gift = value when 'include_gift_message' then li. = value when 'gift_message' then li. = value when 'gift_wrap' then li.gift_wrap = value when 'hide_prices' then li.hide_prices = value end end li.save li.order.calculate resp.success = true render :json => resp end |