微信小程序商城开发之实现商品加入购物车的功能(代码)
yuyutoo 2024-10-13 09:13 6 浏览 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实现特卖商品的流式布局代码
以上就是微信小程序商城开发之实现商品加入购物车的功能(代码)的详细内容,更多请关注其它相关文章!
更多技巧请《转发 + 关注》哦!
相关推荐
- 一篇文章认识JavaScript中的Web API
-
在了解webapi之前先要明白在我们声明变量三个关键字(var、let、const),我们到底该用哪一个?首先排除var,它是老牌的写法,会有很多问题,可以淘汰掉...我们在开发中建议const...
- html5大神结合js带你研究古老读心术,你的心思早被猜透
-
javascript/HTML5课题:javascript开发读心术游戏PS:大爆料!javascript解密读心术游戏背后故事知识点:读心术原理算法独家揭秘,HTML5最新选择器,...
- 纯CSS实现3个圆圈横向排列不断闪烁的Loading特效
-
这个纯CSS实现的Loading特效是一个非常简单而实用的选择,它由三个圆圈横向排列,并不断闪烁。这种动画效果很适合用于页面加载过程中,为用户提供一个愉悦的等待体验。在这个特效的设计中,使用了CSS的...
- 网站建设知识分享系列文章三:符合用户体验的网页设计应如何做?
-
上篇文章向各位讲述了网站建设知识分享系列文章二:如何选择建站公司,今天我们来讲述下建站公司选定后,网站制作流程是怎样的,有哪些需要注意的细节性问题。选定建站公司,签订正规劳务合同后,最先开始的是设计环...
- 「更新」微信小程序 Lottie 动画组件 lottiejs-miniapp V1.1.0 发布
-
lottiejs-miniapp当前版本号:1.1.0npm地址:...
- Web开发基础之jQuery javascript web开发
-
jQuery是一个JavaScript库。jQuery极大地简化了JavaScript编程。jQuery的语法设计可以使开发更加便捷,例如操作文档对象、选择DOM元素、制作动画效果、事件处理、使用Aj...
- Web前端:JavaScript最强总结,最全面的零基础入门教程
-
JavaScript是网景(Netscape)公司开发的一种基于客户端浏览器、面向(基于)对象、事件驱动式的网页脚本语言。JavaScript语言的前身叫作Livescript。JavaScript...
- jQuery 动画制作与特效 jquery的动画函数
-
使用show()和hide()方法在普通的javascript编程中,要实现元素的显示、隐藏通常是利用其CSS的display属性或者visibility属性。在jQuery中提供了show()和hi...
- JavaScript+css实现的登录注册页面web前端html源码
-
大家好,今天给大家介绍一款,JavaScript+css实现的登录注册页面web前端html源码(图1),布局合理。送给大家哦,获取方式在本文末尾。文本框获取焦点动画特效(图2)源码完整,需要的朋友可...
- CSS 3.0+HTML5.0制作各种网页特效
-
?1、C33实现点击图片渐渐放大特效??2、CSS3实现图片全屏背景特效?3、CSS3实现的鼠标移动到图片上不规则放大??3、jQuery+CSS3模拟苹果桌面系统??4、CSS3+jQuery照片...
- js+css实现的按钮悬停动画特效html前端源码,随机元素弹出效果
-
大家好,今天给大家介绍一款,js+css实现的按钮悬停动画特效html页面前端源码,随机元素弹出(图1)。送给大家哦,获取方式在本文末尾。鼠标经过按钮区域的时候,会随机从不同位置上弹出很多小元素,效果...
- Swiper - 免费开源、功能强大的触摸滑动js特效插件
-
简单配置就能实现手机、PC网页中滑动、焦点轮播图、tab切换和触摸导航等大部分功能。js滑动特效插件Swiper是一款纯javascript打造的滑动特效插件,主要用对移动端web开发...
- html5精选特效代码分享(收藏) html酷炫特效
-
在网页设计过程中,我们会经常用到一些HTML5特效代码,下面就是为大家整理分享的一些好看炫酷且实用的HTML5特效代码,可以放心在您的应用程序中使用。一、Canvas跟随鼠标光标动画特效演示、下载地址...
- 玩转Markdown(2)——抽象语法树的提取与操纵
-
上一篇玩转Markdown——数据的分离存储与组件的原生渲染发布,转眼已经鸽了大半年了。最近在操纵mdast生成md文件的时候,心血来潮,把玩转Markdown(2)给补上了。...
- 任由文字肆意流淌,更自由的开源 Markdown 编辑器
-
对于创作平台来说内容编辑器是十分重要的功能,强大的编辑器可以让创作者专注于创作“笔”下生花。而最好取悦程序员创作者的方法之一就是支持Markdown写作,因为大多数程序员都是用Markdown...
你 发表评论:
欢迎- 一周热门
- 最近发表
-
- 一篇文章认识JavaScript中的Web API
- html5大神结合js带你研究古老读心术,你的心思早被猜透
- 纯CSS实现3个圆圈横向排列不断闪烁的Loading特效
- 网站建设知识分享系列文章三:符合用户体验的网页设计应如何做?
- 「更新」微信小程序 Lottie 动画组件 lottiejs-miniapp V1.1.0 发布
- Web开发基础之jQuery javascript web开发
- Web前端:JavaScript最强总结,最全面的零基础入门教程
- jQuery 动画制作与特效 jquery的动画函数
- JavaScript+css实现的登录注册页面web前端html源码
- CSS 3.0+HTML5.0制作各种网页特效
- 标签列表
-
- 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)