微信小程序商城开发之实现商品加入购物车的功能(代码)
yuyutoo 2024-10-13 09:13 3 浏览 0 评论
本篇文章给大家带来的内容是关于微信小程序商城开发之实现商品加入购物车的功能(代码) ,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
看效果
购物车.gif
开发计划
1、商品详情页将商品信息放入缓存
2、购物车页面读取缓存获取商品信息
3、购物车商品计算和删除缓存商品
一、商品详情页将商品信息放入缓存
detail.wxml
<button data-goodid="{{goods.goodsId}}" class="button-addCar" bindtap="addCar" formType="submit">确定</button>
绑定bindtap事件将商品加入购物车。
detail.js
/** * 加入购物车 */ addCar: function (e) { var goods = this.data.goods; goods.isSelect=false; var count = this.data.goods.count; var title = this.data.goods.title; if (title.length > 13) { goods.title = title.substring(0, 13) + '...'; } // 获取购物车的缓存数组(没有数据,则赋予一个空数组) var arr = wx.getStorageSync('cart') || []; console.log("arr,{}", arr); if (arr.length > 0) { // 遍历购物车数组 for (var j in arr) { // 判断购物车内的item的id,和事件传递过来的id,是否相等 if (arr[j].goodsId == goodsId) { // 相等的话,给count+1(即再次添加入购物车,数量+1) arr[j].count = arr[j].count + 1; // 最后,把购物车数据,存放入缓存(此处不用再给购物车数组push元素进去,因为这个是购物车有的,直接更新当前数组即可) try { wx.setStorageSync('cart', arr) } catch (e) { console.log(e) } //关闭窗口 wx.showToast({ title: '加入购物车成功!', icon: 'success', duration: 2000 }); this.closeDialog(); // 返回(在if内使用return,跳出循环节约运算,节约性能) return; } } // 遍历完购物车后,没有对应的item项,把goodslist的当前项放入购物车数组 arr.push(goods); } else { arr.push(goods); } // 最后,把购物车数据,存放入缓存 try { wx.setStorageSync('cart', arr) // 返回(在if内使用return,跳出循环节约运算,节约性能) //关闭窗口 wx.showToast({ title: '加入购物车成功!', icon: 'success', duration: 2000 }); this.closeDialog(); return; } catch (e) { console.log(e) } }
二、购物车页面读取缓存获取商品信息
cart.wxml
<view class="J-shopping-cart-empty" hidden="{{iscart}}"><view class="shopping-cart-empty"> <view class="bg"></view> <view class="cart"></view> <text class='empty-text'>购物车空空如也</text> <p></p> <navigator url="/pages/goods/goods"> <a href="//m.vip.com" class="button button-primary" > 去抢购 </a> </navigator> </view></view> <view class="scroll" hidden="{{hidden}}"><scroll-view class="scroll" scroll-y="true"> <view class="separate"></view> <view wx:for="{{carts}}"> <view class="cart_container"> <view wx:if="{{!item.isSelect}}"> <icon class="item-select" bindtap="switchSelect" data-index="{{index}}" data-id="{{index}}" type="circle" size="20"></icon> </view> <view wx:elif="{{item.isSelect}}"> <icon class="item-select" bindtap="switchSelect" data-index="{{index}}" data-id="{{index}}" type="success" color="#f0145a" size="20"></icon> </view> <image class="item-image" src="{{item.imgUrl}}"></image> <import src="../template/template.wxml" /> <view class="column"> <text class="title">{{item.title}}</text> <image bindtap="delGoods" id="img{{index}}" data-index="{{index}}" src="../../images/del.png"></image> <view class="row"> <text class="sku-price">¥{{item.price * item.count}}</text> <view class="sku"> <template is="quantity" data="{{ ...item,index:index}}" /> </view> </view> </view> </view> <view class="separate"></view> </view></scroll-view></view> <view class="bottom_total" hidden="{{hidden}}"> <view class="bottom_line"></view> <view class="row"> <view wx:if="{{!isAllSelect}}"> <icon class="item-allselect" bindtap="allSelect" type="circle" size="20"></icon> </view> <view wx:elif="{{isAllSelect}}"> <icon class="item-allselect" bindtap="allSelect" type="success" color="#f0145a" size="20"></icon> </view> <text class="small_text">全选</text> <text>合计:¥ </text> <text class="price">{{totalMoney}}</text> <button class="button-red" bindtap="toBuy" formType="submit">去结算 </button> </view></view>
cart.wxss
@import "../template/template.wxss"; page{ background: #f3f4f5; /* font-size: 37.5px; */ } .column image { width:33rpx; height:35rpx; display:inline-block; overflow:hidden; float:right; margin-top: -40rpx; margin-left:400rpx; } .J-shopping-cart-empty{ margin: 0; padding: 0; border: 0; font: inherit; font-size: 100%; vertical-align: baseline; } .shopping-cart-empty { height: 250px; padding-top: 3.2rem; padding-bottom: 1.6rem; background-color: #fff; text-align: center; position: relative; } .shopping-cart-empty .bg{ position: absolute; width: 16.29333rem; height: 6.73333rem; background: transparent url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAm0AAAEXAQMAAADFlOHHAAAABlBMVEUAAAAAAAClZ7nPAAAAAnRSTlMAFRtOKdMAAArmSURBVHja5ZxNiBzHFcf/b2pcvUSt6UkOoYWG7RUE5+LgXtvIIzTsrGXIIST3HAxuWSbxYSEjCI4CiqY2MrIPSywiQhRyiHwLIQdBgiNwcMpswDYoiQkEBDa4iME5ug0JjM1oKofdmf6q7Z7proAgfezp+fXrV69evffqA/jfX5es0tjEKs57xSrusmv1W5VVXIDLNnET9rFFmiMDbhG3hUnLIi52JLP4rWILFnHrFNvEXXJuw7Fnw3Lgo2Ovv1LsomevvzrCRWiLRmoLHJE9M5miBWmtS7C7YGQNF6+7IBK2VCcvuaCr1hr2LeUC16zh3hZt4K49s0Mb+KY13Ajc5jAb2cWF4MDIIq6F2l12VrjTBauN83Thlp/BUXHQBJyjuuD4X4XeFMwI2Fg4hNzr3zvA0dToPTQTRYE/FQmuk//D/XcA/qE2RljjmBn6+o9E4u76ud+O6U+0nunPzcLhogHnyUS6osV8ovXMrLth7F6UJqeykI5U4dcdwDG6L6bFugmH0UI6gy4uA951o3ATdIxut7/AeQbTUqSnZuHgOsrwS2+B6wEQy+GCCYArJhxftEC/gDt+I6IbOyYrkQBTJpyzaAEF5Aa1429E9MaO0U8CQ+OAyuZNR0XckR+rABYbXRG9Oef2C7jObxT94xkzbmj2bBTP/9sr4FxEJFwjjuIjHOVc+z13FVwgKjxbny+Pi2iS9PGWNnWcEQOwvRxu5IkEp7XJYiImlsdNEw80/tyoREkCOVM6EjeW8BR8AHBmSWumW1hALo0bAOPJQdQ6FghuN8T1QXqGHgCagGaGJiYgWgHH4kD0ADgCQ2XAsVVwPpxX35YdAD08OkNTXA/e4DPVARBoLdE14jYwXhq3HrmHuLg4Zh3gQkfHy+J6ko84gLUjSh0EhEHOII/EuTgj2MgB4AJg94yGEl7KKdVlisTAgOvgLOjOHLf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ayuiCuFoFx6MKnCdWki4sw4Wg4ytMXbrg/bJdqBtgaysE4y5aftlE5wZaq+G4W1a42EDr2yvgOFqdMm8boeWDVsCx0gWd2+BdSVgBVzoubqPdnS6PYxWnFWyjvfnyWyvgytfCSjy0iQsr4MqHPQH3FJ5bPnZ8WVXg1k7h3PK4P6EKd2EV3KQi8MXac/j6CllBFc4/hz/D0kXAl54WL1nEdaW0hmNAV473LOJC0bW2orMFbAjYxJ2yiOPA19JzAGFj3Pn0EpRpM1wbeCqN+2tj3NMpHL1sA7fQGGt4REcb+Htqp1bTA0TawI9TuKanpXDgWgrXamgpDLiWik55w5NmSNBLqS3MvOk5ODG1U7hW01N6gt1HUjiGhpZCn7mpAYCh5qqARLxXUgEUiaaTbjTJ4ppOCQYipS+JTsPFsTRh6XCPCQwbbX0IrmUibwU1biTeNB3o44ojmISdywWCGaxNgnIAt1Bz0YfRJRwK+SDiaFccVmLsXPsKsHhYz/kRjEev1MXdxFFnttS5Nvdsqg7+2zZVB39fwN5pXXC3pcVT3cCjyKLqwDdCi6oDC32LqgMLO549Guim80eLONyj2CbuLqxeO3Zxz9vFnbaL+7Jd3Lpd3Ar52X8BXSURa9qZGGgAAAAASUVORK5CYII=); left: 50%; -webkit-transform: translateX(-50%); transform: translateX(-50%); background-size: 100%; top: 0; } .shopping-cart-empty .cart{ width: 5.83333rem; height: 7.46rem; background: transparent url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAANwAAAEaCAMAAABNQsmuAAAAY1BMVEUAAAD6AIr32prUAHU4ByEmCxhWIjd0AEHpAIG6NXLwAIXsAIPjAnvjAH1oNk/4AIqvAGHEAGyonp2npKbpAYKLhon73Kz+66X5357/odv74577pM/yVq7kAH/6AIvzAIf/m9Nba+mLAAAAHXRSTlMA6f7rJRcKgeEdlnA+ukLCnlxgwvOWapTY37Kw+AL3QMkAAAdeSURBVHja7V3bcqM6EARHKEiAMBc7tyX2/3/l2iAwztlUIZCQ1EfzsE9bLnc0PT3TujiKvA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gRIkSIECH0Gg+U5czYxHr78JxZG4dZP5i33IjJnvSjVVtYcvBHJ9NIP8tK/afY1mAzsXNHytlhLws5Wc5/mlCzUUv5fNDZ/z3D16dpTnPukO/9R9R5PBtFmncQsuf5e++iQsvnn8wUxrLSQl4yk+ASy+B+pqXelcssgxsOhE0FRe+9g/yZc7v/TrZ4Mi81Cx0p97Vi/xPc3MKN9/dtLdx8u87AVvKMdamd7nLc0DJx0YeOWxK2OueIZUVh5uhxD48XhcrE8ReAb/tmFJQfqgAAAABJRU5ErkJggg==) no-repeat; background-size: 100%; margin: 0 auto; } .shopping-cart-empty .button { width: 8.46667rem; height: 1.5rem; display: block; margin: 20rpx auto; } .empty-text { font-size: .64667rem; color: #222; margin-top: .53333rem; line-height: .74667rem; font-weight: 400; } .button-primary { border: 1px solid #de3c96; color: #de3c96; text-decoration: none; text-align: center; display: block; border-radius: .21267rem; line-height: 1.5rem; -webkit-appearance: none; background: none; padding: 0 .26667rem; margin: 0; white-space: nowrap; position: relative; text-overflow: ellipsis; font-size: .74333rem; font-family: inherit; cursor: pointer; user-select: none; } .cart_container { display: flex; flex-direction: row; background-color: #FFFFFF; margin-bottom: 10rpx; } .scroll { margin-bottom: 120rpx; } .column { display: flex; flex-direction: column; } .row { display: flex; flex-direction: row; align-items: center; } .sku { margin-left: 100rpx; position: absolute; right: 30rpx; margin-top: 30rpx; } .sku-price { color: red; position: relative; margin-top: 30rpx; } .price { color: red; position: relative; } .title { font-size: 32rpx; margin-top: 40rpx; } .small_text { font-size: 28rpx; margin-right: 40rpx; margin-left: 25rpx; } .item-select { width: 40rpx; height: 40rpx; margin-top: 90rpx; margin-left: 20rpx; } .item-allselect { width: 40rpx; height: 40rpx; margin-left: 20rpx; margin-top:25rpx; } .item-image { width: 180rpx; height: 180rpx; margin: 20rpx; } .bottom_line { width: 100%; height: 2rpx; background: lightgray; } .bottom_total { position: fixed; display: flex; flex-direction: column; bottom: 0; width: 100%; height: 120rpx; line-height: 120rpx; background: white; /* margin-top: 300rpx; */ border-top: 1rpx solid #ccc; z-index: 99; } .button-red { background-color: #f0145a; position: fixed; right: 0; color: white; text-align: center; display: inline-block; font-size: 30rpx; border-radius: 0rpx; width: 30%; height: 120rpx; line-height: 120rpx; /* border: 1rpx solid #ccc; */ }
cart.js
/** * 页面的初始数据 */ data: { carts: [], //数据 iscart: false, hidden: null, isAllSelect: false, totalMoney: 0, }, onShow: function () { // 获取产品展示页保存的缓存数据(购物车的缓存数组,没有数据,则赋予一个空数组) var arr = wx.getStorageSync('cart') || []; 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实现特卖商品的流式布局代码
以上就是微信小程序商城开发之实现商品加入购物车的功能(代码)的详细内容,更多请关注其它相关文章!
更多技巧请《转发 + 关注》哦!
相关推荐
- jQuery VS AngularJS 你更钟爱哪个?
-
在这一次的Web开发教程中,我会尽力解答有关于jQuery和AngularJS的两个非常常见的问题,即jQuery和AngularJS之间的区别是什么?也就是说jQueryVSAngularJS?...
- Jquery实时校验,指定长度的「负小数」,小数位未满末尾补0
-
在可以输入【负小数】的输入框获取到焦点时,移除千位分隔符,在输入数据时,实时校验输入内容是否正确,失去焦点后,添加千位分隔符格式化数字。同时小数位未满时末尾补0。HTML代码...
- 如何在pbootCMS前台调用自定义表单?pbootCMS自定义调用代码示例
-
要在pbootCMS前台调用自定义表单,您需要在后台创建表单并为其添加字段,然后在前台模板文件中添加相关代码,如提交按钮和表单验证代码。您还可以自定义表单数据的存储位置、添加文件上传字段、日期选择器、...
- 编程技巧:Jquery实时验证,指定长度的「负小数」
-
为了保障【负小数】的正确性,做成了通过Jquery,在用户端,实时验证指定长度的【负小数】的方法。HTML代码<inputtype="text"class="forc...
- 一篇文章带你用jquery mobile设计颜色拾取器
-
【一、项目背景】现实生活中,我们经常会遇到配色的问题,这个时候去百度一下RGB表。而RGB表只提供相对于的颜色的RGB值而没有可以验证的模块。我们可以通过jquerymobile去设计颜色的拾取器...
- 编程技巧:Jquery实时验证,指定长度的「正小数」
-
为了保障【正小数】的正确性,做成了通过Jquery,在用户端,实时验证指定长度的【正小数】的方法。HTML做成方法<inputtype="text"class="fo...
- jquery.validate检查数组全部验证
-
问题:html中有多个name[],每个参数都要进行验证是否为空,这个时候直接用required:true话,不能全部验证,只要这个数组中有一个有值就可以通过的。解决方法使用addmethod...
- Vue进阶(幺叁肆):npm查看包版本信息
-
第一种方式npmviewjqueryversions这种方式可以查看npm服务器上所有的...
- layui中使用lay-verify进行条件校验
-
一、layui的校验很简单,主要有以下步骤:1.在form表单内加上class="layui-form"2.在提交按钮上加上lay-submit3.在想要校验的标签,加上lay-...
- jQuery是什么?如何使用? jquery是什么功能组件
-
jQuery于2006年1月由JohnResig在BarCampNYC首次发布。它目前由TimmyWilson领导,并由一组开发人员维护。jQuery是一个JavaScript库,它简化了客户...
- django框架的表单form的理解和用法-9
-
表单呈现...
- jquery对上传文件的检测判断 jquery实现文件上传
-
总体思路:在前端使用jquery对上传文件做部分初步的判断,验证通过的文件利用ajaxFileUpload上传到服务器端,并将文件的存储路径保存到数据库。<asp:FileUploadI...
- Nodejs之MEAN栈开发(四)-- form验证及图片上传
-
这一节增加推荐图书的提交和删除功能,来学习node的form提交以及node的图片上传功能。开始之前需要源码同学可以先在git上fork:https://github.com/stoneniqiu/R...
- 大数据开发基础之JAVA jquery 大数据java实战
-
上一篇我们讲解了JAVAscript的基础知识、特点及基本语法以及组成及基本用途,本期就给大家带来了JAVAweb的第二个知识点jquery,大数据开发基础之JAVAjquery,这是本篇文章的主要...
- 推荐四个开源的jQuery可视化表单设计器
-
jquery开源在线表单拖拉设计器formBuilder(推荐)jQueryformBuilder是一个开源的WEB在线html表单设计器,开发人员可以通过拖拉实现一个可视化的表单。支持表单常用控件...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- mybatis plus (70)
- scheduledtask (71)
- css滚动条 (60)
- java学生成绩管理系统 (59)
- 结构体数组 (69)
- databasemetadata (64)
- javastatic (68)
- jsp实用教程 (53)
- fontawesome (57)
- widget开发 (57)
- vb net教程 (62)
- hibernate 教程 (63)
- case语句 (57)
- svn连接 (74)
- directoryindex (69)
- session timeout (58)
- textbox换行 (67)
- extension_dir (64)
- linearlayout (58)
- vba高级教程 (75)
- iframe用法 (58)
- sqlparameter (59)
- trim函数 (59)
- flex布局 (63)
- contextloaderlistener (56)