Home >Web Front-end >Front-end Q&A >How to calculate price with single selection using JavaScript
With the rapid development of the e-commerce market, users’ needs for purchasing goods are becoming more and more diversified. In order to meet the purchasing needs of users, the calculation of product prices has become more and more complex, among which the calculation of single-select prices is an issue that cannot be ignored. This article will introduce how to calculate the price of single selection using JavaScript.
In most e-commerce websites, the single-select pricing rules are often "bulk pricing" or "quantity tiered pricing". Below we will demonstrate the implementation methods of these two pricing rules with examples.
Bulk pricing means that within the purchase quantity range of the same single product, each quantity corresponds to a price. For example: when purchasing 1-5 items, the price is 10 yuan/item; when purchasing 6-10 items, the price is 8 yuan/item; when purchasing 11 or more items, the price is 6 yuan/item.
The following is a JavaScript code that implements batch pricing:
//定义批量定价规则 var rule = [ {num: 1, price: 10}, {num: 6, price: 8}, {num: 11, price: 6}, ]; //获取单品数量 var quantity = parseInt(document.getElementById('quantity').value); //查找符合条件的价格(以购买1件商品为例) var price = rule.find(function(item) { return quantity <= item.num; }).price; console.log('价格为:' + price);
In this code, we first define the batch pricing rules and passdocument.getElementById('quantity') .value
Get the product quantity input by the user. Then we used JavaScript's find()
method to find the price that matches the purchase quantity in the pricing rules and output it.
Quantity ladder pricing means that within the purchase quantity range of the same single product, each quantity has a fixed price, but as the purchase quantity increases, each quantity Prices will change. For example: when purchasing 1 product, the price is 10 yuan/piece, when purchasing 2 products, the price is 18 yuan/2 pieces, and when purchasing 3 products, the price is 24 yuan/3 pieces.
The following is a JavaScript code that implements quantity tiered pricing:
//定义数量阶梯计价规则 var rule = [ {num: 1, price: 10}, {num: 2, price: 18}, {num: 3, price: 24}, ]; //获取单品数量 var quantity = parseInt(document.getElementById('quantity').value); //计算价格(以购买2件商品为例) var price = 0; rule.forEach(function(item) { if(quantity >= item.num) { var count = parseInt(quantity / item.num); price += count * item.price; quantity -= count * item.num; } }); price += quantity * rule[0].price; console.log('价格为:' + price);
In this code, we first define the quantity tiered pricing rules and passdocument.getElementById('quantity ').value
Get the quantity of goods entered by the user. Then we traverse the pricing rules, calculate the quantity of goods purchased in each price range according to the quantity of goods, and calculate the corresponding total price. Finally, we multiply the remaining quantity by the lowest price and add it to the total price to get the final price.
To summarize, the calculation of single-selection prices is very important in e-commerce websites. Through the introduction of this article, we have learned how JavaScript implements batch pricing and quantity tiered pricing. We can choose the appropriate pricing rules according to the specific situation and realize the function of calculating the price through single selection.
The above is the detailed content of How to calculate price with single selection using JavaScript. For more information, please follow other related articles on the PHP Chinese website!