![](/img/trans.png)
[英]in Nodejs code below, adding to cart is working for the first new items, but, then every item quantity added individually from the other similar item?
[英]Every time I click on a item its getting added to cart.I need to delete previously added items before adding new `item` in angular js
我的问题可能是重复的。但是那些正在使用magento。
在添加新商品之前,我需要帮助来删除购物车中先前添加的商品。
现在确切发生的是每次我单击一个item
,它都会增加item
的数量。但是,我真正需要的只是购物车中要添加的最新项目。 这是我的代码。
service.js
.factory('CartService', [function () {
var svc = {};
svc.saveCart = function(cart){
window.localStorage.setItem('cart', JSON.stringify(cart));
};
svc.loadCart = function(){
var cart = window.localStorage.getItem('cart');
if(!cart){
return { products : [ ] }
}
return JSON.parse(cart);
};
svc.resetCart = function(){
var cart = { products : [ ] };
svc.saveCart(cart);
return cart;
};
svc.getTotal = function(cart){
var out = 0;
if(!cart || !cart.products || !angular.isArray(cart.products)){
return out;
}
for(var i=0; i < cart.products.length; i++){
out += cart.products[i].price;
}
return out;
}
return svc;
}])
controller.js
.controller('FoodCtrl', function($scope,$state,mySharedService, $ionicActionSheet, BackendService, CartService) {
$scope.cart = CartService.loadCart();
$scope.doRefresh = function(){
BackendService.getFood()
.success(function(newItems) {
$scope.products = newItems;
})
.finally(function() {
$scope.$broadcast('scroll.refreshComplete');
});
};
var addProductToCart = function(product){
$scope.cart.products.push(product);
CartService.saveCart($scope.cart);
};
$scope.addProduct = function(product){
$ionicActionSheet.show({
buttons: [
{ text: '<b>Add to produce</b>' }
],
titleText: 'add to cart ' + product.itemName,
cancelText: 'Cancel',
cancel: function() {
// add cancel code if needed ..
},
buttonClicked: function(index) {
if(index == 0){
addProductToCart(product);
return true;
}
return true;
}
});
};
任何回应都将大有帮助。
谢谢。
为此,请首先检查数组是否为空,如果不是,则不为空
喜欢:
if ($scope.cart.products.length !== 0) {
$scope.cart.products= [];
$scope.cart.products.push(product);
} else {
$scope.cart.products.push(product);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.