亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

微信小程序商城開發之怎樣實現商品加入購物車功能

發布時間:2021-01-28 14:00:42 來源:億速云 閱讀:1866 作者:小新 欄目:移動開發

這篇文章將為大家詳細講解有關微信小程序商城開發之怎樣實現商品加入購物車功能,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

看效果

微信小程序商城開發之怎樣實現商品加入購物車功能

購物車.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+7IBK2VCcvuaCr1hr2LeUC16zh4hZt4K49s0Mb+KY13Ajc5jAb2cWF4MDIIq6F2l12VrjTBauN83Thlp/BUXHQBJyjuuD4X4XeFMwI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Nvdsqg7+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/l2iAwztlUIZCQ1EfzsE9bLnc0PT3TujiKvAjKItRgn4fDOyY8ej7cAxKdxHY41IDY3g5Q4BhP2yL/iQ0jLfO0u0WbPWPDWLik6PpIxRzbG0Y5EfEA7nJ81JJPlEopvidwZzBoD3Ddny80aFFEygFb++d6BYN2r5btsHDXW3zhtZK3ehn32K4feG2JuE7xhQwObuno+QHuesJi3Nv7YQauQVq0z17cAMHdF22YAL7A0nJctHtTQvMPpIJCz++HB7YoqiU6iGJ5nqAd3obJ53yD93GGGOKmlHwf14pSxiiFAgfpccmR+40CYhuW7tf5JhGel8v6zH5dNtKcItwgTUNw0dG6SYDRMRaF8Hj5gMElJ+SqmTQNBUZXN8i0q6GrJo3AAxkgQSberWhCCx40uhpazkMfHcLRZgW7jca2VpDRsUYA0y44KyFC7F9UsNWuBkaH7RlhS/kJ2VNJ4B2xECGCkO8p5NDgWAAXwDnXgdXIMh6alBAh9gwB3KBQYKOBIvsMDHrhkN2vYDKEviuM4PupN26hp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y3CbdMp7V//snd0k3RYJd550dCtpXCbdIOHt+sbeZdKx9V2z+6QbjK+XDX8dd12ibRI+I52LA+uavaufpHPWmk0um591dJZ0UuXSLZ/h7MAqVY5v+hBXXSKZldu+mKPWrKyV6bZPcdSaZZ2GrHSUdHLhNmalo2/NypucxdbPcdElku9jpJtfpTm6d2qWlFoY5yTpEt5pWjhJum/h4LqlOuqAY6QbsbVaRminBtakijtdhOtl3B3SUcLHN0IKPZnEXCEdJVncaV23yBGXKCE5n6C12rCNpBPEXuQZnz39lOrDJknXxdbix3NKhc5ToMyp97JSzZ5H4RC0QncLf3QEWasf2jiwWgZ2Q5aZGLwk6VJ7URyPlalXN6XSMUthtq09XvDfmsX8fdfctZnOBOmQf8oAOi/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

/**
   * 頁面的初始數據
   */
  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', []);
    }
  }

關于“微信小程序商城開發之怎樣實現商品加入購物車功能”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

临猗县| 三台县| 武鸣县| 三门峡市| 浦江县| 拉萨市| 黑河市| 潼南县| 淳化县| 龙门县| 公主岭市| 新余市| 宣化县| 芒康县| 白城市| 广德县| 固始县| 巴楚县| 沂源县| 宁强县| 青海省| 大同市| 萍乡市| 沂南县| 滕州市| 金寨县| 虎林市| 双鸭山市| 兴山县| 中牟县| 桐柏县| 南昌县| 锡林郭勒盟| 化州市| 武穴市| 肥东县| 汾阳市| 德阳市| 鄂托克前旗| 财经| 双鸭山市|