How to implement the shopping cart function in WeChat applet? The following article will introduce to you how to implement the shopping cart function in WeChat applet. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Whether it is a shopping mall or catering applet, the transaction needs to be completed by placing an order, so there is an operation to add to the shopping cart. In the past, the shopping cart function was basically implemented through a large number of DOM operations. Since the applet is not based on the web, it cannot create a DOM. WeChat applet implements shopping cart functionIn fact, it is very similar to the usage of vue.js.
First, let’s figure out the needs of the shopping cart.
Single selection, all selection and cancellation, and the total price will be calculated according to the selected products
Increase and decrease of the purchase quantity of a single product
Delete products . When the shopping cart is empty, the page will change to the layout of the empty shopping cart
According to the design drawing, we can first implement a static page. Next, let’s look at what kind of data a shopping cart requires.
First is a product list (carts). The items in the list need: product image (image), product name (title), unit price (price), quantity (num), whether it is selected (selected), Product id (id)
Then select all in the lower left corner, a field (selectAllStatus) is required to indicate whether all are selected
The total price in the lower right corner (totalPrice)
Finally Need to know whether the shopping cart is empty (hasList)
Now that we need these data, we define these first when the page is initialized.
Initialization code:
Page({ data: { carts:[], // 购物车列表 hasList:false, // 列表是否有数据 totalPrice:0, // 总价,初始为0 selectAllStatus:true // 全选状态,默认全选 }, onShow() { this.setData({ hasList:true, // 既然有数据了,那设为true吧 carts:[ {id:1,title:\'新鲜芹菜 半斤\',image:\'/image/s5.png\',num:4,price:0.01,selected:true}, {id:2,title:\'素米 500g\',image:\'/image/s6.png\',num:1,price:0.03,selected:true} ] }); }, })
We usually get the shopping cart list data by requesting the server, so we put it in the life cycle function to assign values to carts. I thought about getting the latest status of the shopping cart every time I enter the shopping cart, and onLoad and onReady are only executed once during initialization, so I need to put the request in the onShow function.
Calculate the total price
Total price = Price of selected product 1 * Quantity Price of selected product 2 * Quantity …
According to the formula, we can get
getTotalPrice() { let carts =this.data.carts; // 获取购物车列表 let total = 0; for(let i = 0;i<carts.length; i++)="{ =" 循环列表得到每个数据<="span=" style=";padding: 0px"> if(carts[i].selected){ // 判断选中才会计算价格 total +=carts[i].num * carts[i].price; // 所有价格加起来 } } this.setData({ // 最后赋值到data中渲染到页面 carts: carts, totalPrice:total.toFixed(2) }); }
This method needs to be called if other operations on the page will cause the total price to change.
Selection event
It is selected when clicked, and becomes unselected when clicked again. In fact, it is changing the selected field. Pass data-index="{{index}}" to pass the index of the current product in the list array to the event.
selectList(e) { const index =e.currentTarget.dataset.index; // 获取data- 传进来的index let carts =this.data.carts; // 获取购物车列表 const selected =carts[index].selected; // 获取当前商品的选中状态 carts[index].selected= !selected; // 改变状态 this.setData({ carts: carts }); this.getTotalPrice(); // 重新获取总价 }
Select all event
Select all is to change the selected quantity of each product according to the all-selected status selectAllStatus
selectAll(e) { let selectAllStatus =this.data.selectAllStatus; // 是否全选状态 selectAllStatus =!selectAllStatus; let carts =this.data.carts; for (let i = 0; i< carts.length; i++) { carts[i].selected = selectAllStatus; // 改变所有商品状态 } this.setData({ selectAllStatus:selectAllStatus, carts: carts }); this.getTotalPrice(); // 重新获取总价 }
Click on the number, add 1 to num, click on the - number, if num > 1, then subtract 1
// 增加数量 addCount(e) { const index =e.currentTarget.dataset.index; let carts =this.data.carts; let num =carts[index].num; num = num + 1; carts[index].num =num; this.setData({ carts: carts }); this.getTotalPrice(); }, // 减少数量 minusCount(e) { const index =e.currentTarget.dataset.index; let carts =this.data.carts; let num =carts[index].num; if(num <= 1){ return false; } num = num - 1; carts[index].num =num; this.setData({ carts: carts }); this.getTotalPrice(); }
Delete the product
Click the delete button to delete the current element from the shopping cart list. If the shopping cart is empty after deletion, change the shopping cart empty flag hasList to false
deleteList(e) { const index =e.currentTarget.dataset.index; let carts =this.data.carts; carts.splice(index,1); // 删除购物车列表里这个商品 this.setData({ carts: carts }); if(!carts.length){ // 如果购物车为空 this.setData({ hasList:false // 修改标识为false,显示购物车为空页面 }); }else{ // 如果不为空 this.getTotalPrice(); //重新计算总价格 } }
Mini program shopping cart function development splits each functional module into separate Development is basically the same as the web and APP development ideas. Although the shopping cart function is relatively simple, there are still many knowledge points involved in the WeChat applet.
Recommendation: " Mini Program Development Tutorial"
The above is the detailed content of How to implement shopping cart function in WeChat applet? (Method introduction). For more information, please follow other related articles on the PHP Chinese website!