Advertisement
Guest User

Untitled

a guest
Oct 20th, 2017
419
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 2.46 KB | None | 0 0
  1. $('.add-to-cart').on('click', function () {
  2.             quanlity = $(this).siblings().find('#qty').val();
  3.             if (quanlity == undefined)
  4.                 quanlity = 1;
  5.             productid = $(this).data('productid');
  6.             $.ajax({
  7.                 url: '/cart/AddToCart/?productId=' + productid + '&quantity=' + quanlity,
  8.                 dataType: 'json',
  9.                 success: function (data) {
  10.                     GetFromCart();
  11.                     if (data.Code == 1)
  12.                         toastr.success(data.Msg + "<br/>Số lượng: " + data.Data.Quantity, { timeOut: 10, preventDuplicates: true });
  13.                     if (data.Error)
  14.                         toastr.error(data.Msg, { timeOut: 10, preventDuplicates: true });
  15.                 }
  16.             });
  17.             return false;
  18.         });
  19.  
  20.         var AddToCart = function (item) {
  21.             itemhtml = $('<li id="product-' + item.item.id + '"><a href="' + item.item.url + '"><img src="' + item.item.imageUrl + '" alt=""></a><button data-productid="' + item.item.id + '" type="button" class="close">×</button><div class="overflow-h"><span>' + item.item.name + '</span><small>' + item.quantity + ' x <span class="digits" style="display:inline">' + item.item.displayPrice + '</span></small></div></li>');
  22.             itemhtml.find('.digits').digits();
  23.             itemhtml.find('.close').on('click', function () {
  24.                 RemoveFromCart($(this).data('productid'));
  25.             });
  26.             $('.shop-badge li.subtotal').before(itemhtml);
  27.         }
  28.         var RemoveFromCart = function (id) {
  29.             $.ajax({
  30.                 url: '/cart/RemoveFromCart/?productId=' + id,
  31.                 dataType: 'json',
  32.                 success: function (data) {
  33.                     $('.shop-badge div.mCSB_container li#product-' + id).remove();
  34.                     $('.shopping-cart table tr#cproduct-' + id).remove();
  35.                 }
  36.             });
  37.         };
  38.         var GetFromCart = function () {
  39.  
  40.             $.ajax({
  41.                 url: '@Url.Action("GetFromCart", "Cart")',
  42.                 dataType: 'json',
  43.                 success: function (data) {
  44.                     $('.shop-badge div.mCSB_container li:not([class="subtotal"]').remove();
  45.                     for (i = 0; i < data.length; i++)
  46.                         AddToCart(data[i]);
  47.                 }
  48.             });
  49.         }
  50.  
  51.         $(document).ready(function () {
  52.             GetFromCart();
  53.         });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement