您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)微信小程序商城開發(fā)之怎樣實(shí)現(xiàn)商品加入購(gòu)物車功能,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
看效果
購(gòu)物車.gif
開發(fā)計(jì)劃
1、商品詳情頁將商品信息放入緩存
2、購(gòu)物車頁面讀取緩存獲取商品信息
3、購(gòu)物車商品計(jì)算和刪除緩存商品
一、商品詳情頁將商品信息放入緩存
detail.wxml
<button data-goodid="{{goods.goodsId}}" class="button-addCar" bindtap="addCar" formType="submit">確定</button>
綁定bindtap事件將商品加入購(gòu)物車。
detail.js
/** * 加入購(gòu)物車 */ 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) + '...'; } // 獲取購(gòu)物車的緩存數(shù)組(沒有數(shù)據(jù),則賦予一個(gè)空數(shù)組) var arr = wx.getStorageSync('cart') || []; console.log("arr,{}", arr); if (arr.length > 0) { // 遍歷購(gòu)物車數(shù)組 for (var j in arr) { // 判斷購(gòu)物車內(nèi)的item的id,和事件傳遞過來的id,是否相等 if (arr[j].goodsId == goodsId) { // 相等的話,給count+1(即再次添加入購(gòu)物車,數(shù)量+1) arr[j].count = arr[j].count + 1; // 最后,把購(gòu)物車數(shù)據(jù),存放入緩存(此處不用再給購(gòu)物車數(shù)組push元素進(jìn)去,因?yàn)檫@個(gè)是購(gòu)物車有的,直接更新當(dāng)前數(shù)組即可) try { wx.setStorageSync('cart', arr) } catch (e) { console.log(e) } //關(guān)閉窗口 wx.showToast({ title: '加入購(gòu)物車成功!', icon: 'success', duration: 2000 }); this.closeDialog(); // 返回(在if內(nèi)使用return,跳出循環(huán)節(jié)約運(yùn)算,節(jié)約性能) return; } } // 遍歷完購(gòu)物車后,沒有對(duì)應(yīng)的item項(xiàng),把goodslist的當(dāng)前項(xiàng)放入購(gòu)物車數(shù)組 arr.push(goods); } else { arr.push(goods); } // 最后,把購(gòu)物車數(shù)據(jù),存放入緩存 try { wx.setStorageSync('cart', arr) // 返回(在if內(nèi)使用return,跳出循環(huán)節(jié)約運(yùn)算,節(jié)約性能) //關(guān)閉窗口 wx.showToast({ title: '加入購(gòu)物車成功!', icon: 'success', duration: 2000 }); this.closeDialog(); return; } catch (e) { console.log(e) } }
二、購(gòu)物車頁面讀取緩存獲取商品信息
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'>購(gòu)物車空空如也</text> <p></p> <navigator url="/pages/goods/goods"> <a href="//m.vip.com" class="button button-primary" > 去搶購(gòu) </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>合計(jì):¥ </text> <text class="price">{{totalMoney}}</text> <button class="button-red" bindtap="toBuy" formType="submit">去結(jié)算 </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+7IBK2VCcvuaCr1hr2LeUC16zh4hZt4K49s0Mb+KY13Ajc5jAb2cWF4MDIIq6F2l12VrjTBauN83Thlp/BUXHQBJyjuuD4X4XeFMwI2Fg4hNzr3zvA0dToPTQTRYE/FQmuk//D/XcA/qE2RljjmBn6+o9E4u76ud+O6U+0nunPzcLhogHnyUS6osV8ovXMrLth7F6UJqeykI5U4dcdwDG6L6bFugmH0UI6gy4uA951o3ATdIxut7/AeQbTUqSnZuHgOsrwS2+B6wEQy+GCCYArJhxftEC/gDt+I6IbOyYrkQBTJpyzaAEF5Aa1429E9MaO0U8CQ+OAyuZNR0XckR+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n5uri7ErUcJKGobXImDmLo3YznINklS0PRMQbSieTVYhsig2nfJdnVQpGKllxzxRCVr6PsjL+iJPyAwNC2i99emtagetjUVphAFC+UOafVeKlcCQAlK0NYfpqxSIe9JKyFwkBlG0GbNFu1ZjcS7l2IcsHWo5dUVHpcFM4KVE6adKGVGeqcGOxPG57dr48THBJr4CLvivLm9ZNrVaLBFC2CdVF+NXcvorCIyndnhe8FLeG7qAig3VTLX9BOBU4v08flPfCVOj5JJzSTS5rWHvHKd/NnJ5M2oIjynA+vD23POlKr3kcgMsyXBfO6/2Ktsi8vVVayuiCuFoFx6MKnCdWki4sw4Wg4ytMXbrg/bJdqBtgaysE4y5aftlE5wZaq+G4W1a42EDr2yvgOFqdMm8boeWDVsCx0gWd2+BdSVgBVzoubqPdnS6PYxWnFWyjvfnyWyvgytfCSjy0iQsr4MqHPQH3FJ5bPnZ8WVXg1k7h4PK4P6EKd2EV3KQi8MXac/j6CllBFc4/hz/D0kXAl54WL1nEdaW0hmNAV473LOJC0bW2orMFbAjYxJ2yiOPA19JzAGFj3Pn0EpRpM1wbeCqN+2tj3NMpHL1sA7fQGGt4REcb+Htqp1bTA0TawI9TuKanpXDgWgrXamgpDLiWik55w5NmSNBLqS3MvOk5ODG1U7hW01N6gt1HUjiGhpZCn7mpAYCh6qqARLxXUgEUiaaTbjTJ4ppOCQYipS+JTsPFsTRh7XCPCQwbbX0IrmUibwU1biTeNB3o44ojmISdywWCGaxNgnIAt1Bz0YfRJRwK+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,i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XKCE5n6C12rCNpBPEXuQZnz39lOrDJknXxdbix3NKhc5ToMyp97JSzZ5H4RC0QncLf3QEWasf2jiwWgZ2Q5aZGLwk6VJ7URyPlalXN6XSMUthtq09XvDfmsX8fdfctZnOBOmQf8oAOi/9enVJNS9jzKWTTQoo6yz/+p7hpZNDnUBeOsyKSeTIyglwYoKu3fiwegnJu9ERbREVgU5mSkko7tp1F57jweMPO6XIE0qxEGYzE7MtsypnyY5h+m+Z/3QxLzuGeacjs+hAH42nJuPW4KU7MO8Gr4UFd4NX3fDtDrDdzwvIM14ULztGAepzhAgRIkSIECH0Gg+U5czYxHr78JxZG4dZP5i33IjJnvSjVVtYcvBHJ9NIP8tK/afY1mAzsXNHytlhLws5Wc5/mlCzUUv5fNDZ/z3D16dpTnPukO/9R9R5PBtFmncQsuf5e++iQsvnn8wUxrLSQl4yk+ASy+B+pqXelcssgxsOhE0FRe+9g/yZc7v/TrZ4Mi81Cx0p97Vi/xPc3MKN9/dtLdx8u87AVvKMdamd7nLc0DJx0YeOWxK2OueIZUVh6uhxD48XhcrE8ReAb/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
/** * 頁面的初始數(shù)據(jù) */ data: { carts: [], //數(shù)據(jù) iscart: false, hidden: null, isAllSelect: false, totalMoney: 0, }, onShow: function () { // 獲取產(chǎn)品展示頁保存的緩存數(shù)據(jù)(購(gòu)物車的緩存數(shù)組,沒有數(shù)據(jù),則賦予一個(gè)空數(shù)組) var arr = wx.getStorageSync('cart') || []; console.info("緩存數(shù)據(jù):"+arr); // 有數(shù)據(jù)的話,就遍歷數(shù)據(jù),計(jì)算總金額 和 總數(shù)量 if (arr.length > 0) { // 更新數(shù)據(jù) this.setData({ carts: arr, iscart: true, hidden: false }); console.info("緩存數(shù)據(jù):" + this.data.carts); }else{ this.setData({ iscart: false, hidden: true, }); } },
三、購(gòu)物車商品計(jì)算和刪除緩存商品
cart.js
//勾選事件處理函數(shù) switchSelect: function (e) { // 獲取item項(xiàng)的id,和數(shù)組的下標(biāo)值 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; //價(jià)錢統(tǒng)計(jì) 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, }) }, // 去結(jié)算 toBuy() { wx.showToast({ title: '去結(jié)算', icon: 'success', duration: 3000 }); this.setData({ showDialog: !this.data.showDialog }); }, //數(shù)量變化處理 handleQuantityChange(e) { var componentId = e.componentId; var quantity = e.quantity; this.data.carts[componentId].count.quantity = quantity; this.setData({ carts: this.data.carts, }); }, /* 減數(shù) */ delCount: function (e) { var index = e.target.dataset.index; console.log("剛剛您點(diǎn)擊了加一"); var count = this.data.carts[index].count; // 商品總數(shù)量-1 if (count > 1) { this.data.carts[index].count--; } // 將數(shù)值與狀態(tài)寫回 this.setData({ carts: this.data.carts }); console.log("carts:" + this.data.carts); this.priceCount(); }, /* 加數(shù) */ addCount: function (e) { var index = e.target.dataset.index; console.log("剛剛您點(diǎn)擊了加+"); var count = this.data.carts[index].count; // 商品總數(shù)量+1 if (count < 10) { this.data.carts[index].count++; } // 將數(shù)值與狀態(tài)寫回 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數(shù)據(jù)對(duì)象 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', []); } }
關(guān)于“微信小程序商城開發(fā)之怎樣實(shí)現(xiàn)商品加入購(gòu)物車功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。