Sha256: 7fa96ab22f35101a8888901d75d01b588141fe658f8b2dd691a4660f54ead1bc

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

class CartController < ApplicationController
  layout :layout

  unloadable

  def index
    @cart = find_cart()
  end

  def update
    cart = find_cart()


    if params.key?('quantity_total_update') && params.key?('cart_item')
      # We are updating the total quantity of multiple items in teh cart. This is likely from teh cart page where
      # the user has updated the quantity text fields and clicked 'Update Cart'
      params['cart_item'].each do |item_id, data|
        cart_item = CartItem.find_by_id(item_id)

        if data.key?('quantity')
          cart.set_item_quantity(cart_item.product, data['quantity'].to_i)
        end
      end
      flash[:notice] = "Your cart has been updated."
    else
      # Adding a given number (quanity) of a particular product. Probably an add to cart from the product page
      product = Product.find_by_id(params[:product_id])
      quantity = (params[:quantity] || 1).to_i
      cart.add(product, quantity)
      flash[:notice] = "#{quantity} #{quantity==1 ? 'item' : 'items'} #{quantity == 1 ? 'has' : 'have'} been added to your <a href='/cart'>cart</a>.".html_safe
    end
    
    redirect_to :back
  end

  def destroy
    session[:cart] = nil
    flash[:notice] = "Your cart has been emptied."
    redirect_to :back
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ecommerce-0.0.2 app/controllers/cart_controller.rb