card: update quantity function
Showing
| ... | ... | @@ -14,7 +14,7 @@ class CardsController < ApplicationController |
| redirect_to cards_path | ||
| else | ||
| 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 card_item[:product_id] == params[:product_id] | ||
| card_items.delete(card_item) | ||
| ... | ... | @@ -24,6 +24,29 @@ class CardsController < ApplicationController |
| end | ||
| end | ||
| end | ||
| elsif params[:card_action] != nil && params[:card_action] == 'update' | ||
|
||
| # Nothing to update | ||
| if @card_infos.empty? | ||
| redirect_to cards_path | ||
| else | ||
| quantity = params[:quantity] | ||
| # Invalid request | ||
| if quantity == nil || quantity.to_i <= 0 | ||
|
||
| 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 | ||
| # if add a product to card | ||
| # first time add to card | ||
| ... | ... | @@ -41,10 +64,6 @@ class CardsController < ApplicationController |
| card_items.each do |card_item| | ||
| # if a product exist in card | ||
| 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 | ||
| break | ||
| end | ||
| ... | ... | |
| ... | ... | @@ -21,21 +21,24 @@ |
| total = 0 | ||
| card_items.each do |card_item| | ||
| product = Product.find(card_item[:product_id]) | ||
| total += product.price | ||
| total += product.price * card_item[:quantity] | ||
| %> | ||
| <tr> | ||
| <td><%= $i %></td> | ||
| <td><%= link_to product.name, product_url(product) %></td> | ||
| <% 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 %> | ||
| <td><%= card_item[:quantity] %></td> | ||
| <td><div class="text-right"><%= card_item[:quantity] %></div></td> | ||
| <% end %> | ||
| <td><%= number_with_delimiter(product.price) %></td> | ||
| <td><%= number_with_delimiter(product.price * card_item[:quantity]) %></td> | ||
| <td><div class="text-right"><%= number_with_delimiter(product.price) %></div></td> | ||
| <td><div class="text-right"><%= number_with_delimiter(product.price * card_item[:quantity]) %></div></td> | ||
| <% if can_edit %> | ||
| <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> | ||
|
||
| <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> | ||
|
||
| </div> | ||
| </td> | ||
| <% end %> | ||
| </tr> | ||
| ... | ... | @@ -54,7 +57,6 @@ |
| <tr> | ||
| <td colspan="6"> | ||
| <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> | ||
|
||
| </div> | ||
| </td> | ||
| ... | ... | |