[];
console.info("缓存数据:"+arr); // 有数据的话,就遍历数据,计算总金额 和 总数量
if (arr.length > 0) {
// 更新数据
this.setData({
carts: arr,
iscart: true,
hidden: false
});
console.info("缓存数据:" + this.data.carts);
}else{
this.setData({
iscart: false,
hidden: true,
});
}
},
三、购物车商品计算和删除缓存商品
cart.js
//勾选事件处理函数
switchSelect: function (e) {
// 获取item项的id,和数组的下标值
var Allprice = 0, i = 0;
let id = e.target.dataset.id,
index = parseInt(e.target.dataset.index);
this.data.carts[index].isSelect = !this.data.carts[index].isSelect; //价钱统计
if (this.data.carts[index].isSelect) {
this.data.totalMoney = this.data.totalMoney + (this.data.carts[index].price * this.data.carts[index].count);
} else {
this.data.totalMoney = this.data.totalMoney - (this.data.carts[index].price * this.data.carts[index].count);
}
//是否全选判断
for (i = 0; i < this.data.carts.length; i++) {
Allprice = Allprice + (this.data.carts[index].price * this.data.carts[index].count);
}
if (Allprice == this.data.totalMoney) {
this.data.isAllSelect = true;
} else {
this.data.isAllSelect = false;
}
this.setData({
carts: this.data.carts,
totalMoney: this.data.totalMoney,
isAllSelect: this.data.isAllSelect,
})
},
//全选
allSelect: function (e) {
//处理全选逻辑
let i = 0;
if (!this.data.isAllSelect) {
this.data.totalMoney = 0;
for (i = 0; i < this.data.carts.length; i++) {
this.data.carts[i].isSelect = true;
this.data.totalMoney = this.data.totalMoney + (this.data.carts[i].price * this.data.carts[i].count);
}
} else {
for (i = 0; i < this.data.carts.length; i++) {
this.data.carts[i].isSelect = false;
}
this.data.totalMoney = 0;
}
this.setData({
carts: this.data.carts,
isAllSelect: !this.data.isAllSelect,
totalMoney: this.data.totalMoney,
})
}, // 去结算
toBuy() {
wx.showToast({
title: '去结算',
icon: 'success',
duration: 3000
});
this.setData({
showDialog: !this.data.showDialog
});
}, //数量变化处理
handleQuantityChange(e) {
var componentId = e.componentId;
var quantity = e.quantity;
this.data.carts[componentId].count.quantity = quantity;
this.setData({
carts: this.data.carts,
});
},
/* 减数 */
delCount: function (e) {
var index = e.target.dataset.index;
console.log("刚刚您点击了加一");
var count = this.data.carts[index].count; // 商品总数量-1
if (count > 1) {
this.data.carts[index].count--;
}
// 将数值与状态写回
this.setData({
carts: this.data.carts
});
console.log("carts:" + this.data.carts);
this.priceCount();
},
/* 加数 */
addCount: function (e) {
var index = e.target.dataset.index;
console.log("刚刚您点击了加+");
var count = this.data.carts[index].count; // 商品总数量+1
if (count < 10) {
this.data.carts[index].count++;
}
// 将数值与状态写回
this.setData({
carts: this.data.carts
});
console.log("carts:" + this.data.carts);
this.priceCount();
},
priceCount: function (e) {
this.data.totalMoney = 0;
for (var i = 0; i < this.data.carts.length; i++) {
if (this.data.carts[i].isSelect == true) {
this.data.totalMoney = this.data.totalMoney + (this.data.carts[i].price * this.data.carts[i].count);
}
}
this.setData({
totalMoney: this.data.totalMoney,
})
},
/* 删除item */
delGoods: function (e) {
this.data.carts.splice(e.target.id.substring(3),1); // 更新data数据对象
if (this.data.carts.length > 0) {
this.setData({
carts: this.data.carts
})
wx.setStorageSync('cart', this.data.carts);
this.priceCount();
} else {
this.setData({
cart: this.data.carts,
iscart: false,
hidden: true,
})
wx.setStorageSync('cart', []);
}
}相关推荐:
微信小程序商城开发之动态API把商品进行分类(代码)
微信小程序商城开发之动态API实现特卖商品的流式布局代码
以上就是微信小程序商城开发之实现商品加入购物车的功能(代码)的详细内容,更多请关注php中文网其它相关文章!
小程序是一种不需要下载安装即可使用的应用,它实现了应用“触手可及”的梦想,用户扫一扫或者搜一下即可打开应用。
关键词:微信小程序商城开发之完成商品加入购物车的技巧(代码)