微信小程序购物车功能实现(干货满满)

article/2025/10/23 8:49:22

微信小程序定制好看的购物车页面,实现购物车功能,希望对您有所帮助!

1. 应用场景
2. 思路分析
3. 代码分析
4. 具体实现代码

效果截图:
在这里插入图片描述

1.应用场景

适用于商城、秒杀、商品购买等类型的小程序,负责将顾客浏览的商品保存下来,方便客户集中比较商品与购买商品。

2.思路分析

实现购物车功能前请思考以下问题:
1.小程序如何布局?使用什么布局能提升页面开发效率??
2.将购物车功能分为四个小功能:(1)一键全选/取消商品 (2)动态添加商品可手动输入 (3)计算结算商品金额 (4)左滑动删除商品

答:(1)在小程序中主要是兼容安卓与ios两种型号手机,在页面开发中可使用flex布局,能极大的提高页面的开发效率。(2)请仔细阅读代码分析,看懂自己也可轻松实现购物车功能 so easy!!!

3.代码分析

1. 一键全选/取消

 allSelect: function (e) {var that = thisvar allSelect = e.currentTarget.dataset.select//判断是否选中 circle是 success否var newList = that.data.slideProductListif (allSelect == "circle") {for (var i = 0; i < newList.length; i++) {newList[i].select = "success"}var select = "success"} else {for (var i = 0; i < newList.length; i++) {newList[i].select = "circle"}var select = "circle"}that.setData({slideProductList: newList,allSelect: select})that.countNum()//计算商品数量that.count()//计算商品金额},

2. 动态添加商品可手动输入

  • a 添加商品
addtion: function (e) {//添加商品var that = thisvar index = e.currentTarget.dataset.indexvar num = e.currentTarget.dataset.numif (num < 99) { //默认峰值99件num++}var newList = that.data.slideProductListnewList[index].num = numthat.setData({goodsNum:num,slideProductList: newList})that.countNum()that.count()},
  • b 减少商品
  subtraction: function (e) {//减少商品var that = thisvar index = e.currentTarget.dataset.indexvar num = e.currentTarget.dataset.numvar newList = that.data.slideProductList//当1件时,再次点击移除该商品if (num == 1) {newList.splice(index, 1)} else {num--newList[index].num = num}that.setData({goodsNum: num,slideProductList: newList})that.countNum()that.count()},
  • c 直接输入
inputNum:function(e){var num = e.detail.value;this.setData({goodsNum:num})},numIputBlur:function(e){var that = this;var num = that.data.goodsNum;var index = e.currentTarget.dataset.index;var newList = that.data.slideProductListif (num == "") { //判空newList[index].num = 1;that.setData({slideProductList: newList})}else if (num < 1) {that.setData({goodsNum: newList[index].num,slideProductList: newList})wx.showToast({title: '亲,该宝贝不能减少了哦~',icon: 'none'})}else if(num>99){that.setData({goodsNum: newList[index].num,slideProductList: newList})wx.showToast({title: '亲,该宝贝最多购买99件哦~',icon: 'none'})}else{newList[index].num = num;that.setData({slideProductList: newList})}that.countNum()that.count()},

3. 计算结算商品金额

count: function () {//计算金额方法var that = thisvar newList = that.data.slideProductListvar newCount = 0for (var i = 0; i < newList.length; i++) {if (newList[i].select == "success") {newCount += newList[i].num * newList[i].price}}that.setData({count: newCount})},  

4. 页面左滑动删除商品

功能后续整理

4. 具体实现代码

1.wxml

 <view class="product-container"><view class="product-list" style='height:{{height}}px'><view class="product-item" wx:for="{{slideProductList}}" wx:for-index="index" wx:key='slideProductList'><slide-delete pid="{{item.id}}" bindaction="handleSlideDelete" wx:key='slideProductList'><view class="product-item-wrap"><icon type="{{item.select}}" size="19" data-index="{{index}}" data-select="{{item.select}}" bindtap="change" color="red" /><view class="product_img"><image src="{{item.url}}" class='goods-img' mode="widthFix"></image></view><view class="product-movable-item"><view class="goods-name">{{item.name}}</view><view class="goods-type">最新款<text>{{item.style}}</text></view><view class="goods-price">{{item.price}}</view></view><view class="product-movable-item product-movable-item-amount"><view class="num-box"><view class="btn-groups"><button class="goods-btn btn-minus" data-index="{{index}}" data-num="{{item.num}}" bindtap="subtraction"></button><input class='num' type='number' data-index="{{index}}" bindblur="numIputBlur" bindinput='inputNum' value='{{item.num}}'></input><button class="goods-btn btn-add" data-index="{{index}}" data-num="{{item.num}}" bindtap="addtion">+</button></view></view></view></view></slide-delete></view></view><view class="cart-fixbar"><view class="allSelect"><icon type="{{allSelect}}" size="19" data-select="{{allSelect}}" bindtap="allSelect" color='#fff' /><view class="allSelect-text">全选</view></view><view class="count"><text>合计:</text>{{count}}</view><view class="order"><view class="orders">去结算<text class="allnum">({{num}})</text></view></view></view>
</view>
<view class="footer"><navigator class="ft_item" url="../shoping/shoping" hover-class="none" open-type='redirect'><image src="../../image/sy_1.png"></image><view class="item_title">首页</view></navigator><navigator url="../classification/classification" hover-class="none" open-type='redirect' class="ft_item"><image src="../../image/fl_1.png"></image><view class="item_title">分类</view></navigator><view class="ft_item"><image src="../../image/gwc.png"></image><view class="item_title">购物车</view></view><navigator hover-class="none" url="../my/my" open-type='redirect' class="ft_item"><image src="../../image/gr_1.png"></image><view class="item_title">我的</view></navigator>
</view>

2.js

const app = getApp()
Page({/*** 页面的初始数据*/data: {goodsNum:'',userInfo: {},hasUserInfo: false,canIUse: wx.canIUse('button.open-type.getUserInfo'),slideProductList: [{id:1,name: '智能手环1111111112222211',url: "../../image/bracelet.jpg",style: "2代",price: "149.5",select: "circle",num: "1",code: "0001",amount: 500},{id: 2,name: "指环支架",url: "../../image/ring.jpg",style: "金色",price: "19.9",select: "circle",code: "0002",num: "1",amount: 500},{id: 3,name: "新款平板电脑",url: "../../image/iphone.png",style: "9.7英寸",price: "100",select: "circle",code: "0003",num: "1",amount: 110},{id: 4,code: "0001",name: "无人机",url: "../../image/uav.jpg",style: "低配版",price: "4999",select: "circle",code: "0004",num: "1",amount: 200},{id: 5,code: "0001",name: "无人机",url: "../../image/uav.jpg",style: "低配版",price: "4999",select: "circle",code: "0004",num: "1",amount: 200},{id: 6,code: "0001",name: "无人机",url: "../../image/uav.jpg",style: "低配版",price: "4999",select: "circle",code: "0004",num: "1",amount: 200},],allSelect: "circle",num: 0,count: 0,lastX: 0,lastY: 0,text: "没有滑动",},change: function (e) {var that = thisvar index = e.currentTarget.dataset.indexvar select = e.currentTarget.dataset.selectif (select == "circle") {var stype = "success"} else {var stype = "circle"}var newList = that.data.slideProductListnewList[index].select = stypethat.setData({slideProductList: newList})that.countNum()that.count()},addtion: function (e) {var that = thisvar index = e.currentTarget.dataset.indexvar num = e.currentTarget.dataset.num//默认99件if (num < 99) {num++}var newList = that.data.slideProductListnewList[index].num = numthat.setData({goodsNum:num,slideProductList: newList})that.countNum()that.count()},inputNum:function(e){var num = e.detail.value;this.setData({goodsNum:num})},numIputBlur:function(e){var that = thisvar num = that.data.goodsNumvar index = e.currentTarget.dataset.indexvar newList = that.data.slideProductListif (num == "") { //盘空newList[index].num = 1;that.setData({slideProductList: newList})}else if (num < 1) {that.setData({goodsNum: newList[index].num,slideProductList: newList})wx.showToast({title: '亲,该宝贝不能减少了哦~',icon: 'none'})}else if(num>99){that.setData({goodsNum: newList[index].num,slideProductList: newList})wx.showToast({title: '亲,该宝贝最多购买99件哦~',icon: 'none'})}else{newList[index].num = num;that.setData({slideProductList: newList})}that.countNum()that.count()},//减法subtraction: function (e) {var that = thisvar index = e.currentTarget.dataset.indexvar num = e.currentTarget.dataset.numvar newList = that.data.slideProductListif (num == 1) {//当数量为1件时,再次点击移除该商品newList.splice(index, 1)} else {num--newList[index].num = num}that.setData({goodsNum: num,slideProductList: newList})that.countNum()that.count()},//全选allSelect: function (e) {var that = thisvar allSelect = e.currentTarget.dataset.select //先判断是否选中var newList = that.data.slideProductListconsole.log(newList)if (allSelect == "circle") {for (var i = 0; i < newList.length; i++) {newList[i].select = "success"}var select = "success"} else {for (var i = 0; i < newList.length; i++) {newList[i].select = "circle"}var select = "circle"}that.setData({slideProductList: newList,allSelect: select})that.countNum()that.count()},countNum: function () { //计算数量var that = thisvar newList = that.data.slideProductListvar allNum = 0for (var i = 0; i < newList.length; i++) {if (newList[i].select == "success") {allNum += parseInt(newList[i].num)}}parseIntthat.setData({num: allNum})},count: function () {//计算金额方法var that = thisvar newList = that.data.slideProductListvar newCount = 0for (var i = 0; i < newList.length; i++) {if (newList[i].select == "success") {newCount += newList[i].num * newList[i].price}}that.setData({count: newCount})},/*** 生命周期函数--监听页面加载*/onLoad: function (options) {var width=wx.getSystemInfoSync().windowWidthvar height=wx.getSystemInfoSync().windowHeightheight=height-55-53;this.setData({height:height})},/*** 生命周期函数--监听页面初次渲染完成*/onReady: function () {},/*** 生命周期函数--监听页面显示*/onShow: function () {},/*** 生命周期函数--监听页面隐藏*/onHide: function () {},/*** 生命周期函数--监听页面卸载*/onUnload: function () {},/*** 页面相关事件处理函数--监听用户下拉动作*/onPullDownRefresh: function () {},/*** 页面上拉触底事件的处理函数*/onReachBottom: function () {},/*** 用户点击右上角分享*/onShareAppMessage: function () {}
})

## 写在最后如果你觉得这篇文章写得不错或者对您有用的话请帮忙点赞加收藏,毕竟原创不易;如果您觉得文章有什么地方写得不对或者需要改进的地方,欢迎通过评论私聊博主!

http://chatgpt.dhexx.cn/article/8QRKU6k6.shtml

相关文章

微信小程序微商城(八):缓存实现商品购物车功能

IT实战联盟博客&#xff1a;http://blog.100boot.cn 上一篇&#xff1a;微信小程序微商城&#xff08;七&#xff09;&#xff1a;动态API实现商品分类 看效果 购物车.gif 开发计划 1、商品详情页将商品信息放入缓存 2、购物车页面读取缓存获取商品信息 3、购物车商品计算…

微信小程序开发一个小型商城(六、购物车页面)

上一篇文章&#xff1a;微信小程序开发一个小型商城&#xff08;五、商品详情&#xff09; 当我们在商品详情界面中点击添加购物后&#xff0c;会跳转到购物车界面&#xff0c;购物车界面是一个tabbar&#xff0c;在跳转的时候需要加上ope-type。看下购物车的静态页面把&#x…

微信小程序实现一个购物车页面的简易列表效果

本文只是简单的模仿天猫APP的购物车列表的样式效果&#xff0c;并实现了部分事件功能&#xff0c;功能并不完善&#xff0c;请降低期待观看。 天猫APP的购物车效果&#xff1a; 小程序模仿的实现效果&#xff1a; wxml部分的代码&#xff1a; <view wx:if"{{!isCartEmp…

【Python之pymysql库学习】一、分析fetchone()、fetchmany()、fetchall()(保姆级图文+实现代码)

目录 实现效果实现思路实现代码总结 欢迎关注 『Python之pymysql库学习』 系列&#xff0c;持续更新中 欢迎关注 『Python之pymysql库学习』 系列&#xff0c;持续更新中 实现效果 实现思路 其实有半数代码是创建数据库和创建数据表并插入数据这些环境配置部分我都写好了&…

fetchone、fetchall

fetchone(): 该方法获取下一个查询结果集。结果集是一个对象,读取一行结果&#xff0c;读取完指向下一行&#xff0c;到空为止 fetchall():接收全部的返回结果行&#xff0c;到空为止 fetchone() &#xff1a; 返回单个的元组&#xff0c;也就是一条记录(row)&#xff0c;如果没…

python fetchall方法_Python连接MySQL并使用fetchall()方法过滤特殊字符

python3.3从mysql里取出的数据莫名其妙有括号和逗号每天跟自己喜欢的人一起&#xff0c;通电话&#xff0c;旅行&#xff0c;重复一个承诺和梦想&#xff0c;听他第二十八次提起童年往事&#xff0c;每年的同一天和他庆祝生日&#xff0c;每年的情人节圣诞节除夕&#xff0c;也…

pdo fetchAll

作用 fetchAll()方法是获取结果集中的所有行.其返回值是一个包含结果集中所有数据的二维数组。 PDOStatement::fetchAll ([ int $fetch_style [, mixed $fetch_argument[, array$ctor_args array() ]]] ) fetch_style:控制结果的返回方式 PDO::FETCH_ASSOC 关联数组形式 PD…

记录一个常用函数fetchall()的使用过程

fetchall() 作用是返回多个元组&#xff0c;即对应数据库里的多条数据概念&#xff1b; 常见用法是 cursor.execute(‘select * from table’) value cursor.fetchall() 此时&#xff0c;print(value)则会输出以下二维元组&#xff0c;如下图 拓展&#xff1a; 同类函数fet…

Python从Oracle数据库中获取数据——fetchall(),fetchone(),fetchmany()函数功能分析

Python从Oracle数据库中获取数据——fetchall(),fetchone(),fetchmany()函数功能分析 一、fetchall()&#xff0c;fetchone()&#xff0c;fetchmany()简单介绍 1、fetchall()函数,它的返回值是多个元组,即返回多个行记录,如果没有结果,返回的是() 2、fetchone()函数,它的返回…

KITTI数据集可视化(一):点云多种视图的可视化实现

如有错误&#xff0c;恳请指出。 在本地上&#xff0c;可以安装一些软件&#xff0c;比如&#xff1a;Meshlab&#xff0c;CloudCompare等3D查看工具来对点云进行可视化。而这篇博客是将介绍一些代码工具将KITTI数据集进行可视化操作&#xff0c;包括点云鸟瞰图&#xff0c;FOV…

KITTI数据集的点云格式转PCD格式

参考文章&#xff1a;https://blog.csdn.net/xinguihu/article/details/78922005 KITTI数据集应该不用多做介绍了&#xff0c;基本上做自动驾驶的都知道这个东西。最近本人用到这个数据集想看看里面的点云长什么模样&#xff0c;却发现有点别扭&#xff0c;没有直接可以看的工…

使用kitti数据集实现自动驾驶——发布照片、点云、IMU、GPS、显示2D和3D侦测框

本次内容主要是使用kitti数据集来可视化kitti车上一些传感器&#xff08;相机、激光雷达、IMU&#xff09;采集的资料以及对行人和车辆进行检测并在图像中画出行人和车辆的2D框、在点云中画出行人和车辆的3D框。 首先先看看最终实现的效果&#xff1a; 自动驾驶视频 看了上面的…

KITTI数据集-label解析笔记

笔记摘自&#xff1a;KITTI数据集--label解析与传感器间坐标转换参数解析_苏源流的博客-CSDN博客 KITTI数据集是自动驾驶领域最知名的数据集之一。 一、kitti数据集&#xff0c;label解析 16个数代表的含义&#xff1a; 第1个字符串&#xff1a;代表目标的类别 Car, Van, Tru…

16个车辆信息检测数据集收集汇总(简介及链接)

16个车辆信息检测数据集收集汇总&#xff08;简介及链接) 转载自&#xff1a;https://blog.csdn.net/u014546828/article/details/109089621?utm_mediumdistribute.pc_relevant.none-task-blog-baidujs_baidulandingword-1&spm1001.2101.3001.4242 目录 1. UA-DETRAC …

双目网络公开数据集的特性

文章目录 概述SceneFlowKITTI 2012 & 2015ETH3D 2017Middlebury 2014 概述 参考文章&#xff1a;Rethinking Training Strategy in Stereo Matching 主流双目公开数据集有&#xff1a;SceneFlow、KITTI、ETH3D、MB。 各个双目网络主流训练数据视差分布的直方图&#xff1a;…

KITTI数据集下载及介绍

KITTI数据集下载及介绍 KITTI数据集由德国卡尔斯鲁厄理工学院和丰田美国技术研究院联合创办&#xff0c;是目前国际上最大的自动驾驶场景下的计算机视觉算法评测数据集。该数据集用于评测立体图像(stereo)&#xff0c;光流(optical flow)&#xff0c;视觉测距(visual odometry…

KITTI 数据集--下载链接

Visual Odometry / SLAM Evaluation 2012 数据集主页&#xff1a;The KITTI Vision Benchmark Suite (cvlibs.net) 里程计基准由22个立体序列组成&#xff0c;以无损失png格式保存。 11个具有真实轨迹的序列&#xff08;00-10&#xff09;用于训练&#xff0c;11个没有真实…

KITTI数据集下载及解析

KITTI数据集下载及解析 W.P. Xiao, Vision group&#xff0c;SHUSV 版本更新时间更新内容作者1V 1.02020.01.09完成主体内容W.P. Xiao2 文章目录 KITTI Dataset1 简介1.1 数据采集平台1.2 坐标系 2 数据解析2.1 image文件2.2 velodyne文件2.3 calib文件2.4 label文件 3 KITTI可…

无人驾驶之KITTI数据集介绍与应用(一)——数据组织方式介绍

本系列博客旨在介绍无人驾驶领域中颇负盛名的KITTI公开数据集&#xff0c;首先整体介绍该数据集的由来、数据组织方式、官方开发工具的使用&#xff0c;重点详细介绍其中对于Object、Tracking和raw data的数据使用&#xff0c;主要分享了我在使用这些数据集时开发的一些工具&am…

waymo数据集总结

参考资料 官网&#xff1a; https://waymo.com/open/data/perception/#lidar-data 文章&#xff1a; https://arxiv.org/pdf/1912.04838.pdf github&#xff1a; https://github.com/waymo-research/waymo-open-dataset colab教程&#xff1a; https://colab.research.google.…