当前位置:   article > 正文

uniapp微信小程序_购物车_下单页面_小程序购物车

小程序购物车

 先说下整体逻辑以方便总体理解
 1、首先画出下单页面
 2、此次画出结算价格页面
 3、怎么点击下完单变成结算页面?其实就是把下单页面的信息传递给结算页面就行
 问题难点?
点击加号的时候把物品加入一个数组传到下单页面,但是点击的时候不能把所有物品加上,需要把重复的物品转换成数量
点击减号为0的时候需要减去商品,使用过滤api过滤掉次数为0的就相当于删除次数为0的商品
最后将物品总价使用computed一算就可以

 一、解决购物车加购的时候重复问题

  1. mergeDuplicateItems(cartItems) {
  2. // 创建一个空对象来存储合并后的商品
  3. const mergedItems = {};
  4. // 遍历购物车中的每个商品
  5. cartItems.forEach(item => {
  6. // 使用商品名称作为键来查找是否存在该商品
  7. const existingItem = mergedItems[item.name];
  8. if (existingItem) {
  9. // 把这边的点击次数传出去
  10. existingItem.content = item.content;
  11. } else {
  12. // 如果商品不存在,则将其添加到合并后的对象中
  13. mergedItems[item.name] = {
  14. ...item
  15. };
  16. }
  17. });
  18. // 将合并后的对象转换回数组
  19. // 把汉字就去掉了
  20. const mergedCartItems = Object.values(mergedItems);
  21. return mergedCartItems;
  22. },

以上把加购重复的商品加购进去

  1. mergedItems[item.name] = {
  2. ...item
  3. };

这个解释一下

  1. mergedCartItems是这个但是得转换一下
  2. {
  3. exampleName1: { name: 'exampleName1', value: 'value1' },
  4. exampleName2: { name: 'exampleName2', value: 'value2' },
  5. exampleName3: { name: 'exampleName3', value: 'value3' }
  6. }
  7. Object.values(mergedItems);使用这个转换一下就下面这种了
  8. [
  9. { name: 'exampleName1', value: 'value1' },
  10. { name: 'exampleName2', value: 'value2' },
  11. { name: 'exampleName3', value: 'value3' }
  12. ]

Object.values() - JavaScript | MDN (mozilla.org)这是api官方解释

二、加购商品 减少商品

  1. // 加号
  2. increaseCount(index) {
  3. // 加号次数增加
  4. this.food[index].content++
  5. // 加号显示
  6. this.food[index].showDelete = true;
  7. // 将点的餐加入购买的订单
  8. this.cart.push(this.food[index])
  9. // 有重复的处理成一个
  10. this.mergedCart = this.mergeDuplicateItems(this.cart);
  11. //过滤,当点击的次数等于0的时候将次数不等于0的过滤出来,就是相当于删除了次数等于0的商品
  12. let filteredFood = this.mergedCart.filter(item => item.content !== 0);
  13. // 转换成字符串
  14. this.cartstring = JSON.stringify(filteredFood)
  15. },
  16. // 减号
  17. deletegoods(index) {
  18. this.food[index].content--
  19. if (this.food[index].content > 0) {
  20. // 将加完之后的结果传入mergeDuplicateItems函数减的话就是把次数传过去
  21. this.mergedCart = this.mergeDuplicateItems(this.cart);
  22. let filteredFood = this.mergedCart.filter(item => item.content !== 0);
  23. this.cartstring = JSON.stringify(filteredFood)
  24. }
  25. if (this.food[index].content == 0) {
  26. this.food[index].showDelete = false;
  27. //过滤,当点击的次数等于0的时候将次数不等于0的过滤出来,就是相当于删除了次数等于0的商品
  28. let filteredFood = this.food.filter(item => item.content !== 0);
  29. this.mergedCart = this.mergeDuplicateItems(filteredFood);
  30. this.cartstring = JSON.stringify(this.mergedCart)
  31. }
  32. }

1.首先定义一个传过去得数组cart再把需要得商品加到购物车里面cart

  1. data:{
  2. cart: [],
  3. }
  4. this.cart.push(this.food[index])
  5. // 有重复的处理成一个
  6. this.mergedCart = this.mergeDuplicateItems(this.cart);
  7. //过滤,当点击的次数等于0的时候将次数不等于0的过滤出来,就是相当于删除了次数等于0的商品
  8. let filteredFood = this.mergedCart.filter(item => item.content !== 0);
  9. // 转换成字符串
  10. this.cartstring = JSON.stringify(filteredFood)

let filteredFood = this.mergedCart.filter(item => item.content !== 0);还得过滤一下不然把之前没有的还会加进去,减商品也一样。

三、算出总价格 

  1. <!-- 加购的食物 -->
  2. <view class="addFood">
  3. <view class="foodDetail" v-for="(item,index) in food">
  4. <image :src="item.imgUrl" class="foodImagel"></image>
  5. <view class="Afternoon">
  6. <view class="">
  7. <view class="AfternoonText">
  8. {{item.name}}
  9. </view>
  10. <view class="recommend">
  11. 默认:×{{item.content}}
  12. </view>
  13. </view>
  14. <view class="">
  15. <view class="foodMoney">
  16. <text class="symbol"></text><text>{{item.money}}</text>
  17. </view>
  18. </view>
  19. </view>
  20. </view>
  21. </view>
  22. <!-- 底部支付 -->
  23. <view class="foot">
  24. <view class="foodMoney">
  25. <text class="symbol"></text><text>{{addPrice}}</text>
  26. </view>
  27. <view class="lijiyuyue" @click="lijiyuyue1">
  28. 立即支付
  29. </view>
  30. </view>
  31. computed: {
  32. // 价格的总价
  33. addPrice() {
  34. var sum = 0
  35. this.food.forEach(el => {
  36. sum = el.money * el.content+sum
  37. })
  38. return sum
  39. }
  40. },

 这个简单:computed定义总的价格addprice,然后直接引用这个变量,就是数量乘以价格加上之前累积得就可以了

 四、完结

大哥们要是有更简单可以一起探讨学习下

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/517149
推荐阅读
相关标签
  

闽ICP备14008679号