Mini Program to Implement the Complete Shopping Cart

Mini Program to Implement the Complete Shopping Cart

The mini program implements a complete shopping cart [select all/deselect to calculate the amount/add and subtract to calculate the quantity and amount] for your reference. The specific contents are as follows

1. wxml page code module

<view wx:if="{{hasList}}">
  <view class="order_list">
    <view class="order" wx:for="{{list}}" wx:key="{{index}}">
      <view class="xuanze" wx:if="{{item.selected}}" catchtap="selectList" data-index="{{index}}">
        <image src="/images/serch/xuanze.png" />
      </view>
      <view class="xuanze" catchtap="selectList" data-index="{{index}}" wx:else>
        <image src="/images/serch/gouxuan.png" />
      </view>
      <!--List product images-->
      <view class="order_img">
        <image src="{{item.image}}" />
      </view>
      <view class="order_text">
        <view class="text_top">
          <!--List Title-->
          <view class="title">{{item.title}}</view>
          <view class="detel" catchtap="deletes" data-index="{{index}}">
            <image src="/images/serch/detel.png" />
          </view>
        </view>
        <!--Specifications-->
        <view class="size">Specifications: {{item.pro_name}}</view>
        <view class="text_bottom">
          <!--Price-->
          <view class="money">¥{{item.price}}</view>
          <!--Add or subtract product quantity-->
          <view class="number">
            <!--Minus button-->
            <view class="reduce" catchtap="btn_minus" data-obj="{{obj}}" data-index="{{index}}">
              <!--Button Image-->
              <image src="/images/serch/jian-1.png" />
            </view>
            <!--Quantity-->
            <view class="numb">{{item.num}}</view>
            <!--Add button-->
            <view class="add" catchtap="btn_add" data-index="{{index}}">
              <!--Button Image-->
              <image src="/images/serch/add-1.png" />
            </view>
          </view>
        </view>
      </view>
    </view>
  </view>

  <!--Fixed bottom-->
  <view class="buy">
    <view class="buy_top">
      <view class="top_left">
        <view class="left_img" catchtap="selectAll" wx:if="{{selectAllStatus}}">
          <image src="/images/serch/gouxuan.png" />
        </view>
        <view class="left_img" catchtap="selectAll" wx:else>
          <image src="/images/serch/gouxuan.png" />
        </view>
        <view class="left_name">Select All</view>
      </view>
      <view class="top_left">
        <view class="left_img">
          <image src="/images/serch/fenxiang.png" />
        </view>
        <view class="left_name">Share</view>
      </view>
    </view>
    <view class="buy_bottom">
      <view class="buy_left">
        <view class="heji">Total:¥{{totalPrice}}</view>
      </view>
      <view class="buy_right">
        <!--Submit order-->
        <view class="liji " catchtap="btn_submit_order">Buy Now</view>
        <view class="liji two active">Appointment for fitting</view>
      </view>
    </view>
  </view>
</view>
<!--No order in shopping cart-->
<view wx:else>
  <view class="list_none">The shopping cart is empty~</view>
</view>

2. Style Code

page {
  background-color: rgba(238, 238, 238, 0.5);
}

.order {
  height: 238rpx;
  background-color: #fefeff;
  margin: 27rpx;
  border-radius: 4rpx;
  display: flex;
  align-items: center;
}

.xuanze {
  width: 40rpx;
  height: 40rpx;
  /* background-color: darkgoldenrod; */
  border-radius: 50%;
  margin: 0 11rpx;
}

.select image {
  width: 100%;
  height: 100%;
  display: block;
  border-radius: 50%;
}

.order_img {
  width: 180rpx;
  height: 180rpx;
}

.order_img image {
  width: 100%;
  height: 100%;
  display: block;
}

.order_text {
  margin-left: 20rpx;
  width: 58%;
  height: 180rpx;
}

.text_top {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title {
  width: 70%;
  font-size: 28rpx;
  color: #4b5248;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1;
  overflow: hidden;
}

.detel {
  width: 30rpx;
  height: 30rpx;
}

.detel image {
  width: 100%;
  height: 100%;
  display: block;
}

.size {
  font-size: 24rpx;
  color: #a8ada6;
}

.text_bottom {
  display: flex;
  margin-top: 50rpx;
  align-items: center;
  justify-content: space-between;
}

.money {
  font-size: 30rpx;
  color: #a5937f;
}

.number {
  display: flex;
  justify-content: space-around;
  align-items: center;
  width: 170rpx;
}

.reduce {
  width: 46rpx;
  height: 46rpx;
}

.reduce image {
  width: 100%;
  height: 100%;
  display: block;
}

.numb {
  font-size: 30rpx;
  color: #a5937f;
}

.add {
  width: 46rpx;
  height: 46rpx;
}

.add image {
  width: 100%;
  height: 100%;
  display: block;
}

/*Buy button*/

.buy {
  height: 180rpx;
  width: 696rpx;
  position: fixed;
  left: 27rpx;
  bottom: 41rpx;
  background-color: #555555f3;
  border-radius: 4rpx;
}

.buy_top {
  border-bottom: 1px solid rgb(98, 98, 99);
  height: 75rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.top_left {
  display: flex;
  align-items: center;
}

.left_img {
  width: 37rpx;
  height: 37rpx;
  margin: 11rpx;
}

.left_img image {
  width: 100%;
  height: 100%;
  display: block;
}

.left_name {
  font-size: 24rpx;
  color: #fefeff;
  margin-right: 29rpx;
}

.buy_bottom {
  display: flex;
  height: 104rpx;
  justify-content: space-between;
  align-items: center;
  padding: 0rpx 30rpx 0rpx 12rpx;
}

.buy_left {
  font-size: 26rpx;
  color: #fff;
}

.buy_right {
  display: flex;
  align-items: center;
}

.liji {
  width: 180rpx;
  height: 70rpx;
  border: 2rpx solid rgba(248, 248, 248, 1);
  box-sizing: border-box;
  border-radius: 4rpx;
  line-height: 70rpx;
  text-align: center;
  font-size: 26rpx;
  color: #FEFEFF;
}
.two{
  margin-left: 12rpx;
}
.active{
  background-color: #A5937F;
  border: none;
}

3. js code module

Page({

  /**
   * Initial data of the page */
  data: {
    hasList: true, //Default display list data //Product list data list: [{
        id: 1,
        title: 'Gardener's Anti-Wrinkle Essence',
        image: '/images/serch/2.png',
        pro_name: "30ml",
        num: 1,
        price: 180,
        selected: true
      },
      {
        id: 2,
        title: 'Evelyn Rose Hand Cream',
        image: '/images/serch/1.png',
        pro_name: "25g",
        num: 1,
        price: 62,
        selected: true
      },
      {
        id: 2,
        title: 'Oatmeal Goat Milk Soothing Hand Cream',
        image: '/images/serch/2.png',
        pro_name: "75ml",
        num: 1,
        price: 175,
        selected: true
      }
    ],
    //Amount totalPrice: 0, //Total price, initially 0
    //Select All Status selectAllStatus: true, //Select All Status, all selected by default},

  /**
   * Life cycle function--listen for page loading*/
  onLoad: function(options) {

  },

  /**
   * Life cycle function--monitor page display*/
  onShow: function() {
    wx.showToast({
      title: 'Loading',
      icon: "loading",
      duration: 1000
    })
    //Price method this.count_price();

  },

  /** Current product selection event*/
  selectList(e) {
    var that = this;
    //Get the selected radio index var index = e.currentTarget.dataset.index;
    //Get the product list data var list = that.data.list;
    //Select all by default that.data.selectAllStatus = true;
    //Loop array data, judge -- selected/unselected [selected]
    list[index].selected = !list[index].selected;
    //If all array data is selected[true], select all for (var i = list.length - 1; i >= 0; i--) {
      if (!list[i].selected) {
        that.data.selectAllStatus = false;
        break;
      }
    }
    // Re-render data that.setData({
      list: list,
      selectAllStatus: that.data.selectAllStatus
    })
    // Call the method to calculate the amount that.count_price();
  },

  // delete deletes(e) {
    var that = this;
    // Get the index const index = e.currentTarget.dataset.index;
    // Get product list data let list = this.data.list;
    wx.showModal({
      title: 'Tips',
      content: 'Are you sure to delete?',
      success: function(res) {
        if (res.confirm) {
          // Delete index from 1
          list.splice(index, 1);
          // Page rendering data that.setData({
            list: list
          });
          // If the data is empty if (!list.length) {
            that.setData({
              hasList: false
            });
          } else {
            // Call the amount rendering data that.count_price();
          }
        } else {
          console.log(res);
        }
      },
      fail: function(res) {
        console.log(res);
      }
    })
  },

  /** Shopping cart full selection event*/
  selectAll(e) {
    // Select all ICONs by default let selectAllStatus = this.data.selectAllStatus;
    // true ----- false
    selectAllStatus = !selectAllStatus;
    // Get product data let list = this.data.list;
    // Loop through the list to determine whether the data is selected for (let i = 0; i < list.length; i++) {
      list[i].selected = selectAllStatus;
    }
    // Page re-rendering this.setData({
      selectAllStatus: selectAllStatus,
      list: list
    });
    // Calculate the amount method this.count_price();
  },

  /** Binding plus quantity event*/
  btn_add(e) {
    // Get the clicked index const index = e.currentTarget.dataset.index;
    // Get product data let list = this.data.list;
    // Get the number of products let num = list[index].num;
    // Click to increment num = num + 1;
    list[index].num = num;
    // Re-render --- show the new quantity this.setData({
      list: list
    });
    // Calculate the amount method this.count_price();
  },

  /**
   * Bind quantity reduction event */
  btn_minus(e) {
    // // Get the clicked index const index = e.currentTarget.dataset.index;
    // const obj = e.currentTarget.dataset.obj;
    // console.log(obj);
    // Get product data let list = this.data.list;
    // Get the number of products let num = list[index].num;
    // Check if num is less than or equal to 1 return; Click invalid if (num <= 1) {
      return false;
    }
    // else num is greater than 1, click the subtraction button--
    num = num - 1;
    list[index].num = num;
    // Render the page this.setData({
      list: list
    });
    // Call the method to calculate the amount this.count_price();
  },

  // Submit order btn_submit_order() {
    var that = this;
    console.log(that.data.totalPrice);

    //Call for payment // wx.requestPayment(
    // {
    // 'timeStamp': '',
    // 'nonceStr': '',
    // 'package': '',
    // 'signType': 'MD5',
    // 'paySign': '',
    // 'success': function (res) { },
    // 'fail': function (res) { },
    // 'complete': function (res) { }
    // })
    wx.showModal({
      title: 'Tips',
      content: 'Total amount -' + that.data.totalPrice + "Not yet developed",
    })
  },

  /**
   * Calculate total price */
  count_price() {
    // Get product list data let list = this.data.list;
    //Declare a variable to receive the array list price
    let total = 0;
    // Loop through the list to get each data for (let i = 0; i < list.length; i++) {
      // Determine whether to select and calculate the price if (list[i].selected) {
        // add up all the prices count_money
        total += list[i].num * list[i].price;
      }
    }
    //Finally assign the value to data and render it to the page this.setData({
      list: list,
      totalPrice: total.toFixed(2)
    });
  },

})

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • WeChat applet shopping cart simple example
  • WeChat Mini Program Practice: Shopping Cart Implementation Code Example
  • WeChat applet implements the functions of selecting and deselecting all in a multiple-choice box and deleting selected items in the shopping cart
  • WeChat applet to implement shopping cart code example
  • WeChat Mini Program Shopping Cart Function
  • WeChat applet implements shopping cart function
  • Summary of WeChat applet shopping cart, parent-child component value transfer and calc considerations
  • Mini Program Quadratic Bezier Curve to Realize the Curve Flying-in Effect of Shopping Cart Items
  • WeChat applet uses swiper+css to realize shopping cart product deletion function
  • Python implements shopping cart shopping applet

<<:  Linux series of commonly used operation and maintenance commands (summary)

>>:  Mac installation mysqlclient process analysis

Recommend

Detailed explanation of moment.js time and date processing

Monday to Sunday time format conversion (Y --- ye...

Summary and analysis of commonly used Docker commands and examples

Table of contents 1. Container lifecycle manageme...

Solution to forgetting MySQL root password in MACOS

MySQL is a relational database management system ...

Minimalistic website design examples

Web Application Class 1. DownForEveryoneOrJustMe ...

Docker-compose installation yml file configuration method

Table of contents 1. Offline installation 2. Onli...

Typical cases of MySQL index failure

Table of contents Typical Cases Appendix: Common ...

MySQL uses binlog logs to implement data recovery

MySQL binlog is a very important log in MySQL log...

Usage of the target attribute of the html tag a

1: If you use the tag <a> to link to a page,...

Node+socket realizes simple chat room function

This article shares the specific code of node+soc...

How to use docker+devpi to build local pypi source

Some time ago, I needed to use pip downloads freq...

How to automatically number the results of MYSQL query data

Preface In fact, I have never encountered this ki...

Form submission refresh page does not jump source code design

1. Design source code Copy code The code is as fol...

Import CSS files using judgment conditions

Solution 1: Use conditional import in HTML docume...

MySQL query redundant indexes and unused index operations

MySQL 5.7 and above versions provide direct query...