微信小程序实现购物车功能,包含完整小程序代码和运行效果截图

couponInfoListMap=goods.couponInfoListMap;

}

}

goodsPrice = parseFloat(goodsPrice).toFixed(2);

let receivePerson = res.data[0];

this.setData({

currentLogisticsText: (logisticsList != undefined && logisticsList.length > 0) ? logisticsList[0].logisticsName : “”,

receiveName: receivePerson.receiveName != undefined ? receivePerson.receiveName : “”,

receiveMobile: receivePerson.receiveMobile != undefined ? receivePerson.receiveMobile : “”,

receiveAddress: receivePerson.receiveAddress != undefined ? receivePerson.receiveAddress : “”,

goodsPrice: goodsPrice,

goodsTotalPrice: goodsPrice,

couponInfoListMap: couponInfoListMap,

goodsNum: goodsNum

});

this.countTotalPrice();

}

}

})

}

},

countTotalPrice() {

//计算商品价格

let goodsPrice= 0.00

let goodsTotalPrice = 0.00

for (let goods of this.data.goodsList) {

//goodsPrice += (goods.goodsAmount * goods.goodsPrice)

goodsPrice += (1 * goods.goodsPrice)

//console.log(“goodsPrice=” + goodsPrice+“//goodsPrice=”+goods.goodsPrice);

}

goodsPrice = parseFloat(goodsPrice).toFixed(2);

goodsTotalPrice = parseFloat(goodsPrice).toFixed(2);

//console.log(“goodsTotalPrice=” + goodsTotalPrice);

//判断是否使用优惠劵

let couponInfoListMap=this.data.couponInfoListMap

let userCouponInfo=null;

if(couponInfoListMap.length>=1){

for (let couponInfo of couponInfoListMap) {

//console.log(“couponInfo=” + JSON.stringify(couponInfo));

if(couponInfo.useConditionsContent<=goodsTotalPrice){

if(userCouponInfo==null){//只有1张优惠劵满足条件

userCouponInfo=couponInfo;

}else{//有多张优惠劵满足条件

if(couponInfo.couponAmount>userCouponInfo.couponAmount){

userCouponInfo=couponInfo;

}

}

}

  • 23
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序源码(含截图购物车微信小程序
微信小程序实现购物车功能代码主要分为两部分:前端和后端。前端代码实现用户界面的展示和交互,后端代码实现数据存储和处理。 前端代码: 1. 在页面的 .json 文件中添加购物车数据和购物车总价的变量 ``` { "data": { "cartList": [], // 购物车数据 "cartTotalPrice": 0 // 购物车总价 } } ``` 2. 在页面的 .wxml 文件中渲染购物车数据 ``` <view wx:for="{{cartList}}" wx:key="index"> <view>{{item.name}}</view> <view>{{item.price}}</view> <view>{{item.count}}</view> <view>{{item.totalPrice}}</view> </view> ``` 3. 绑定加入购物车事件,将商品数据添加到购物车数据中,并计算购物车总价 ``` onAddCart: function(event) { const item = event.currentTarget.dataset.item; let cartList = this.data.cartList; let index = cartList.findIndex(i => i.id === item.id); if (index === -1) { cartList.push({ id: item.id, name: item.name, price: item.price, count: 1, totalPrice: item.price }); } else { let count = cartList[index].count + 1; let totalPrice = count * item.price; cartList[index] = { ...cartList[index], count, totalPrice }; } let cartTotalPrice = cartList.reduce((total, i) => total + i.totalPrice, 0); this.setData({ cartList, cartTotalPrice }); } ``` 4. 绑定移除购物车事件,将商品数据从购物车数据中删除,并计算购物车总价 ``` onRemoveCart: function(event) { const item = event.currentTarget.dataset.item; let cartList = this.data.cartList; let index = cartList.findIndex(i => i.id === item.id); if (index !== -1) { cartList.splice(index, 1); } let cartTotalPrice = cartList.reduce((total, i) => total + i.totalPrice, 0); this.setData({ cartList, cartTotalPrice }); } ``` 后端代码: 1. 创建一个云数据库集合 cart,用于存储购物车数据 2. 在云函数中编写添加购物车数据的函数 ``` const cloud = require('wx-server-sdk') cloud.init() const db = cloud.database() exports.main = async (event, context) => { const { id, name, price } = event try { const res = await db.collection('cart').where({ id }).get() if (res.data.length > 0) { const count = res.data[0].count + 1 const totalPrice = count * price await db.collection('cart').doc(res.data[0]._id).update({ data: { count, totalPrice } }) } else { const count = 1 const totalPrice = price await db.collection('cart').add({ data: { id, name, price, count, totalPrice } }) } return { success: true } } catch (err) { return { success: false, errMsg: err.message } } } ``` 3. 在云函数中编写获取购物车数据的函数 ``` const cloud = require('wx-server-sdk') cloud.init() const db = cloud.database() exports.main = async (event, context) => { try { const res = await db.collection('cart').get() return { success: true, data: res.data } } catch (err) { return { success: false, errMsg: err.message } } } ``` 4. 在页面的 .js 文件中调用云函数,实现前端与后端的数据交互 ``` onAddCart: function(event) { const item = event.currentTarget.dataset.item; wx.cloud.callFunction({ name: 'add-to-cart', data: item, success: res => { console.log(res); this.getCartList(); }, fail: err => { console.log(err); } }); }, getCartList: function() { wx.cloud.callFunction({ name: 'get-cart-list', success: res => { console.log(res); let cartList = res.result.data; let cartTotalPrice = cartList.reduce((total, i) => total + i.totalPrice, 0); this.setData({ cartList, cartTotalPrice }); }, fail: err => { console.log(err); } }); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值