Commit 58a82bcb by vulehuan

card: update quantity function

parent 22099897
...@@ -29,6 +29,14 @@ ...@@ -29,6 +29,14 @@
}); });
}); });
}); });
//
$('.btn-update-card-quantity').unbind('click').bind('click', function(e){
e.preventDefault();
var trTag = $(this).closest("tr");
var quantity = trTag.find('input.txt-quantity').val();
var requestUrl = $(this).attr('href') + '&quantity=' + quantity;
window.location.href = requestUrl;
});
}); });
})(jQuery); })(jQuery);
jQuery.noConflict(); jQuery.noConflict();
\ No newline at end of file
...@@ -461,4 +461,7 @@ footer { ...@@ -461,4 +461,7 @@ footer {
thead { thead {
font-weight: bold; font-weight: bold;
} }
}
.btn-update-card-quantity {
margin-bottom: 3px;
} }
\ No newline at end of file
...@@ -14,7 +14,7 @@ class CardsController < ApplicationController ...@@ -14,7 +14,7 @@ class CardsController < ApplicationController
redirect_to cards_path redirect_to cards_path
else else
card_items = @card_infos[:card_items] card_items = @card_infos[:card_items]
card_items.each do |card_item, key| card_items.each do |card_item|
Please register or sign in to reply
# if a product exist in card # if a product exist in card
if card_item[:product_id] == params[:product_id] if card_item[:product_id] == params[:product_id]
card_items.delete(card_item) card_items.delete(card_item)
...@@ -24,6 +24,29 @@ class CardsController < ApplicationController ...@@ -24,6 +24,29 @@ class CardsController < ApplicationController
end end
end end
end end
elsif params[:card_action] != nil && params[:card_action] == 'update'
Please register or sign in to reply
# Nothing to update
if @card_infos.empty?
redirect_to cards_path
else
quantity = params[:quantity]
# Invalid request
if quantity == nil || quantity.to_i <= 0
Please register or sign in to reply
redirect_to cards_path
else
card_items = @card_infos[:card_items]
card_items.each_with_index do |card_item, key|
# if a product exist in card
if card_item[:product_id] == params[:product_id]
card_item[:quantity] = quantity.to_i
card_items[key] = card_item
@card_infos = { card_items: card_items, customer_info: @card_infos[:customer_info] }
session[:SHOPPING_CARD_SESSION_NAME] = @card_infos
end
end
redirect_to cards_path
end
end
else else
# if add a product to card # if add a product to card
# first time add to card # first time add to card
...@@ -41,10 +64,6 @@ class CardsController < ApplicationController ...@@ -41,10 +64,6 @@ class CardsController < ApplicationController
card_items.each do |card_item| card_items.each do |card_item|
# if a product exist in card # if a product exist in card
if card_item[:product_id] == params[:product_id] if card_item[:product_id] == params[:product_id]
# card_item[:quantity] += 1
# @card_infos = { card_items: card_items, customer_info: @card_infos[:customer_info] }
# session[:SHOPPING_CARD_SESSION_NAME] = @card_infos
# redirect_to cards_path
found = true found = true
break break
end end
......
...@@ -21,21 +21,24 @@ ...@@ -21,21 +21,24 @@
total = 0 total = 0
card_items.each do |card_item| card_items.each do |card_item|
product = Product.find(card_item[:product_id]) product = Product.find(card_item[:product_id])
total += product.price total += product.price * card_item[:quantity]
%> %>
<tr> <tr>
<td><%= $i %></td> <td><%= $i %></td>
<td><%= link_to product.name, product_url(product) %></td> <td><%= link_to product.name, product_url(product) %></td>
<% if can_edit %> <% if can_edit %>
<td><input type="text" class="form-control" value="<%= card_item[:quantity] %>" /></td> <td><input type="text" class="form-control txt-quantity" value="<%= card_item[:quantity] %>" /></td>
<% else %> <% else %>
<td><%= card_item[:quantity] %></td> <td><div class="text-right"><%= card_item[:quantity] %></div></td>
<% end %> <% end %>
<td><%= number_with_delimiter(product.price) %></td> <td><div class="text-right"><%= number_with_delimiter(product.price) %></div></td>
<td><%= number_with_delimiter(product.price * card_item[:quantity]) %></td> <td><div class="text-right"><%= number_with_delimiter(product.price * card_item[:quantity]) %></div></td>
<% if can_edit %> <% if can_edit %>
<td> <td>
<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> <div class="text-center">
<a href="<%= cards_path %>?product_id=<%= card_item[:product_id] %>&card_action=update" class="btn btn-info btn-update-card-quantity"><span class="glyphicon glyphicon-plus"></span> Update quantity</a>
Please register or sign in to reply
<a href="<%= cards_path %>?product_id=<%= card_item[:product_id] %>&card_action=remove" class="btn btn-danger" dada="{:confirm=>"Do you want to remove this product?"}"><span class="glyphicon glyphicon-remove"></span> Remove</a>
Please register or sign in to reply
</div>
</td> </td>
<% end %> <% end %>
</tr> </tr>
...@@ -54,7 +57,6 @@ ...@@ -54,7 +57,6 @@
<tr> <tr>
<td colspan="6"> <td colspan="6">
<div class="text-center"> <div class="text-center">
<input class="btn btn-default" type="submit" name="btn[update]" value="Update quantity" />
<a class="btn btn-primary" href="<%= url_for(action: 'checkout') %>">Check out</a> <a class="btn btn-primary" href="<%= url_for(action: 'checkout') %>">Check out</a>
Please register or sign in to reply
</div> </div>
</td> </td>
......
...@@ -3,8 +3,6 @@ ...@@ -3,8 +3,6 @@
<h2 class="sprite-2"> <h2 class="sprite-2">
Cards<span class="sprite-2"></span> Cards<span class="sprite-2"></span>
</h2> </h2>
<form> <%= render partial: 'cards/item_list', locals: { card_infos: @card_infos, can_edit: true } %>
<%= render partial: 'cards/item_list', locals: { card_infos: @card_infos, can_edit: true } %>
</form>
<div class="clearfix"></div> <div class="clearfix"></div>
</div> </div>
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