小程序添加购物车

  //添加购物车
  savecart: function(res) {
    var that = this;
    var cart = wx.getStorageSync('cart') || [];
    var exist = cart.find(function (ele) {
      return ele.id === res.currentTarget.dataset.id;//判断购物车内是否存在
    })
    if (exist){
      exist.num = parseInt(exist.num)+parseInt(res.currentTarget.dataset.num);//存在增加数量
    }else{
      cart.push(res.currentTarget.dataset);//不存在增加种类
    }
    console.log(cart);
    wx.setStorage({
      key: 'cart',
      data: cart,
      success: function (res) {
        //添加购物车的消息提示框
        wx.showToast({
          title: "加入购物车成功",
          icon: "success",
          durantion: 2000
        })
      }
    })
    this.setData({
      hid: "hid",
    })
  }

猜你喜欢

转载自www.cnblogs.com/wangyinqiuqian/p/12418812.html