2016-05-13 43 views
0

我使用下面的腳本將產品添加到購物車並刪除,其中im顯示計數多少添加和刪除後有多少...留在我的腳本當我刪除計數顯示,並刷新頁面計數後顯示我在哪裏錯過了邏輯...產品添加到購物車和刪除計數丟失,同時刪除opencart中的項目

// Cart add remove functions 
var cart = { 
    'add': function(product_id, quantity) { 
     $.ajax({ 
      url: 'index.php?route=checkout/cart/add', 
      type: 'post', 
      data: 'product_id=' + product_id + '&quantity=' + (typeof(quantity) != 'undefined' ? quantity : 1), 
      dataType: 'json', 
      beforeSend: function() { 
       $('#cart > button').button('loading'); 
      }, 
      complete: function() { 
       $('#cart > button').button('reset'); 
      },   
      success: function(json) { 
       $('.alert, .text-danger').remove(); 

       if (json['redirect']) { 
        location = json['redirect']; 
       } 

       if (json['success']) { 
        $('#content').parent().before('<div class="alert alert-success"><i class="fa fa-check-circle"></i> ' + json['success'] + '<button type="button" class="close" data-dismiss="alert">&times;</button></div>'); 

        // Need to set timeout otherwise it wont update the total 
        setTimeout(function() { 
         $('#cart > button').html('<span id="cart-total"><i class="fa fa-shopping-cart"></i> ' + json['total'] + '</span>'); 
        }, 100); 

        $('html, body').animate({ scrollTop: 0 }, 'slow'); 

        $('#cart > ul').load('index.php?route=common/cart/info ul li'); 
       } 
      } 
     }); 
    }, 
    'update': function(key, quantity) { 
     $.ajax({ 
      url: 'index.php?route=checkout/cart/edit', 
      type: 'post', 
      data: 'key=' + key + '&quantity=' + (typeof(quantity) != 'undefined' ? quantity : 1), 
      dataType: 'json', 
      beforeSend: function() { 
       $('#cart > button').button('loading'); 
      }, 
      complete: function() { 
       $('#cart > button').button('reset'); 
      },   
      success: function(json) { 
       if(json['total']){ 
        var total= json['total']; 
       } 
       else{ 
        var total=""; 
       } 
       // Need to set timeout otherwise it wont update the total 
       setTimeout(function() { 
        $('#cart > button').html('<span id="cart-total"><i class="fa fa-shopping-cart"></i> ' + json['total'] + '</span>'); 
       }, 100); 

       if (getURLVar('route') == 'checkout/cart' || getURLVar('route') == 'checkout/checkout') { 
        location = 'index.php?route=checkout/cart'; 
       } else { 
        $('#cart > ul').load('index.php?route=common/cart/info ul li'); 
       } 
      } 
     }); 
    }, 
    'remove': function(key) { 
     $.ajax({ 
      url: 'index.php?route=checkout/cart/remove', 
      type: 'post', 
      data: 'key=' + key, 
      dataType: 'json', 
      beforeSend: function() { 
       $('#cart > button').button('loading'); 
      }, 
      complete: function() { 
       $('#cart > button').button('reset'); 
      },   
      success: function(json) { 
       // Need to set timeout otherwise it wont update the total 
       if(json['total']){ 
        var total= json['total']; 
       } 
       else{ 
        var total=""; 
       } 
       setTimeout(function() { 
        $('#cart > button').html('<i class="fa fa-shopping-basket"></i>Cart<span id="cart-total">' + total + '</span>'); 
       }, 100); 

       if (getURLVar('route') == 'checkout/cart' || getURLVar('route') == 'checkout/checkout') { 
        location = 'index.php?route=checkout/cart'; 
       } else { 
        $('#cart > ul').load('index.php?route=common/cart/info ul li'); 
       } 
      } 
     }); 
    } 
} 

回答

0

我想你需要修改代碼中的「刪除」部分:下面 變化:

setTimeout(function() { 
        $('#cart > button').html('<i class="fa fa-shopping-basket"></i>Cart<span id="cart-total">' + total + '</span>'); 
       }, 100); 

至:

setTimeout(function() { 
        $('#cart > button').html('<span id="cart-total"><i class="fa fa-shopping-cart"></i> ' + json['total'] + '</span>'); 
       }, 100);