This is slightly different but still on the same subject.
So, I got the customer to log in, but… he doesn’t have a cart that
belongs to him. Which means if another customer logs in, they see the
other’s cart. I want to associate the cart with a customer to also be
able to record the customer’s id in the orders table. but… I don’t
know how to do that.
Other problems that I have with my cart are:
- Update doesn’t happen
- checkout page suddenly doesn’t appear
Will you have a look at how I can associate the cart with the logged
in customer? (And if you can at the other questions/problems)
My cart.rb:
class Cart < ActiveRecord::Base
has_many :cart_items
has_many :products, :through => :cart_items
belongs_to :customer
def total
cart_items.inject(0) {|sum, n| n.price * n.amount + sum}
end
def add(product_id)
items = cart_items.find_all_by_product_id(product_id)
product = Product.find(product_id)
if items.size < 1
ci = cart_items.create(:product_id => product_id,
:amount => 1,
:price => product.price)
else
ci = items.first
ci.update_attribute(:amount, ci.amount + 1)
end
ci
end
def remove(product_id)
ci = cart_items.find_by_product_id(product_id)
if ci.amount > 1
ci.update_attribute(:amount, ci.amount - 1)
else
CartItem.destroy(ci.id)
end
return ci
end
def update(product_id)
ci = cart_items.find_by_product_id(product_id)
ci.update_attribue(:amount => new_qty.to_i )
end
end
My cart_controller:
class CartController < ApplicationController
before_filter :initialize_cart
before_filter :authorize, :except => [“login”]
def authorize
return true if @c
flash[:notice] = “To place your order please login”
redirect_to :controller => “catalog”
end
def login
# examine the form data for “name” and “password”
pw,name = params[:customer].values_at(*%w{password name})
# Retrieve the customer record for the name and store it in a
variable ‘c’
c = Customer.find_by_name(name)
# if such record exists, and it’s password matches the password
from the form
if c && Digest::SHA1.hexdigest(pw) == c.password
# start a session with the customer id
@session[‘customer’] = c.id
if @cart.products.empty?
redirect_to :controller => "catalog"
else
redirect_to :action => "view_cart"
end
else
# otherwise report an error
flash[:notice] = "Invalid Login"
redirect_to :controller => "catalog"
end
end
def logout
@session[‘customer’] = nil
redirect_to :controller => “catalog”
end
def index
end
def add
@product = Product.find(params[:id])
if request.post?
@item = @cart.add(params[:id])
flash[:notice] = “Added #{@item.product.title} to
cart.”
redirect_to :controller => “catalog”
else
render
end
end
def update
@product = Product.find(params[:id])
new_qty = params[:new_qty]
if request.post?
@item = @cart.update(params[:id], new_qty)
flash[:notice] = “Updated #{@item.product.title}'s
quantity.”
redirect_to :action => “view_cart”
else
render
end
end
def remove
@product = Product.find(params[:id])
if request.post?
@item = @cart.remove(params[:id])
flash[:notice] = “Removed #{@item.product.title}”
redirect_to :action => “view_cart”
else
render
end
end
def clear
if request.post?
@cart.cart_items.destroy_all
flash[:notice] = “Cleared the cart”
redirect_to :controller => “catalog”
else
render
end
end
def view_cart
@page_title = “Shopping Cart for #{@c.name}”
@order = Order.new
if @cart.products.empty?
flash[:notice] = “Your shopping cart is empty! Please add
something to it before proceeding to checkout.”
redirect_to :controller => ‘catalog’
end
end
def checkout
@page_title = “Checkout”
@order = Order.new(params[:order])
@order.customer_ip = request.remote_ip
@order.customer_id = @session[‘customer’]
populate_order
if @order.save
if @order.process
flash[:notice] = 'Your order has been submitted, and will be
processed immediately.’
session[:order_id] = @order.id
# Empty the cart
@cart.cart_items.destroy_all
redirect_to :action => ‘thank_you’
else
flash[:notice] = “Error while placing order.
‘#{@order.error_message}’”
render :action => ‘view_cart’
end
else
render :action => ‘view_cart’
end
end
def thank_you
@page_title = ‘Thank You!’
end
def populate_order
for cart_item in @cart.cart_items
order_item = OrderItem.new(
:product_id => cart_item.product_id,
:price => cart_item.price,
:amount => cart_item.amount
)
@order.order_items << order_item
end
end
end
And _item.rhtml which part of the view_cart.rhtml:
<%=h item.product.sku %> |
<%= link_to item.product.title, :action => "show",
:controller => "catalog", :id => item.product.id %> |
<%= pluralize(item.amount, "pc", "pcs") %> |
$<%= two_dec(item.price * item.amount) %> |
<%= form_tag :controller => "cart", :action => "update", :id =>
item.product, :with => "new_qty" %>
<%= text_field_tag "new_qty", "", "size" => 4 %>
<%= end_form_tag %>
|
<%= button_to "x", :controller => "cart", :action => "remove", :id
=> item.product %> |
Now tell me the truth, how messy is my code? where are my mistakes?
Thanks,
Elle