Commit 2ddb6d64 by vulehuan

remove product function

parent 688d4832
...@@ -7,33 +7,53 @@ class CardsController < ApplicationController ...@@ -7,33 +7,53 @@ class CardsController < ApplicationController
@card_infos = session[:SHOPPING_CARD_SESSION_NAME] @card_infos = session[:SHOPPING_CARD_SESSION_NAME]
end end
if params[:product_id] != nil if params[:product_id] != nil
# first time add to card # if remove a product from card
if @card_infos.empty? if params[:card_action] != nil && params[:card_action] == 'remove'
product = Product.find(params[:product_id]) # Nothing to delete
card_items = Array.new if @card_infos.empty?
card_items.push({ product_id: params[:product_id], quantity: 1 }) redirect_to cards_path
customer_info = Hash.new else
@card_infos = { card_items: card_items, customer_info: customer_info } card_items = @card_infos[:card_items]
session[:SHOPPING_CARD_SESSION_NAME] = @card_infos card_items.each do |card_item, key|
redirect_to cards_path # if a product exist in card
if card_item[:product_id] == params[:product_id]
card_items.delete(card_item)
@card_infos = { card_items: card_items, customer_info: @card_infos[:customer_info] }
session[:SHOPPING_CARD_SESSION_NAME] = @card_infos
redirect_to cards_path
end
end
end
else else
card_items = @card_infos[:card_items] # if add a product to card
card_items.each do |card_item, key| # first time add to card
# if a product exist in card if @card_infos.empty?
if card_item[:product_id] == params[:product_id] product = Product.find(params[:product_id])
card_item[:quantity] += 1 card_items = Array.new
card_items[key] = card_item card_items.push({ product_id: params[:product_id], quantity: 1 })
@card_infos = { card_items: card_items, customer_info: @card_infos[:customer_info] } customer_info = Hash.new
session[:SHOPPING_CARD_SESSION_NAME] = @card_infos @card_infos = { card_items: card_items, customer_info: customer_info }
redirect_to cards_path session[:SHOPPING_CARD_SESSION_NAME] = @card_infos
end redirect_to cards_path
else
card_items = @card_infos[:card_items]
card_items.each do |card_item, key|
# if a product exist in card
if card_item[:product_id] == params[:product_id]
card_item[:quantity] += 1
card_items[key] = card_item
@card_infos = { card_items: card_items, customer_info: @card_infos[:customer_info] }
session[:SHOPPING_CARD_SESSION_NAME] = @card_infos
redirect_to cards_path
end
end
# if a product not exist in card
card_items.push({ product_id: params[:product_id], quantity: 1 })
customer_info = @card_infos[:customer_info]
@card_infos = { card_items: card_items, customer_info: customer_info }
session[:SHOPPING_CARD_SESSION_NAME] = @card_infos
redirect_to cards_path
end end
# if a product not exist in card
card_items.push({ product_id: params[:product_id], quantity: 1 })
customer_info = @card_infos[:customer_info]
@card_infos = { card_items: card_items, customer_info: customer_info }
session[:SHOPPING_CARD_SESSION_NAME] = @card_infos
redirect_to cards_path
end end
end end
end end
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<% if @card_infos.empty? %> <% if @card_infos.empty? || @card_infos[:card_items].empty? %>
<tr><td colspan="6"><a href="<%= products_path %>">Click here to order</a></td></tr> <tr><td colspan="6"><a href="<%= products_path %>">Click here to order</a></td></tr>
<% <%
else else
...@@ -34,7 +34,7 @@ ...@@ -34,7 +34,7 @@
<td><%= number_with_delimiter(product.price) %></td> <td><%= number_with_delimiter(product.price) %></td>
<td><%= number_with_delimiter(product.price * card_item[:quantity]) %></td> <td><%= number_with_delimiter(product.price * card_item[:quantity]) %></td>
<td> <td>
<a href="" class="btn btn-default">Remove</a> <a href="<%= cards_path %>?product_id=<%= card_item[:product_id] %>&card_action=remove" class="btn btn-default" dada="{:confirm=>"Do you want to remove this product?"}">Remove</a>
</td> </td>
</tr> </tr>
<% <%
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment