简体   繁体   English

如何使用 Javascript 在我的页面上收集价格以添加到总数中?

[英]How can I use Javascript to gather prices on my page to add to total?

I have a product page that recently had a product add-on tool made for it.我有一个产品页面,它最近有一个产品附加工具。 I am modifying the code to change it's format a bit.我正在修改代码以稍微改变它的格式。 There is a "add to cart" button bar that displays the total price before adding to cart.有一个“添加到购物车”按钮栏,显示添加到购物车之前的总价格。

底部推车栏

I used .innerHTML to match the price of the top of the page for consistency, but I am trying to include the price of the product addons once selected.我使用 .innerHTML 来匹配页面顶部的价格以保持一致性,但我试图在选择后包含产品插件的价格。

Current code for bottom bar:底栏的当前代码:

 var selector1 = ".top-bar"; var el1 = document.querySelector(selector1); var selector2 = ".bottom-bar"; var el2 = document.querySelector(selector2); var totalPriceSelected = ''; function getPrice() { el2.innerHTML = el1.innerHTML; }; document.addEventListener('variant:priceChange', ()=>{ getPrice(); });

The add-on tool is a vue app which I am a little new to.附加工具是一个 vue 应用程序,我对它有点陌生。 I managed to get to the point that I can get the addons to print the prices to the console once selected:我设法让插件在选择后将价格打印到控制台:

 watch: { selected: function(new_value) { setTimeout(function(){ var priceSelected = document.querySelectorAll(".price_selected"); var strippedPriceSelected = priceSelected; for(var i=0;i<strippedPriceSelected.length;i++){ var totalPriceSelected = priceSelected[i].innerHTML; console.log(totalPriceSelected); } }, 10); this.product.selected = new_value; //this.$emit('selected', this.product, new_value); }
控制台日志

Currently, once the add-on products are selected, the vue adds the class of ".price_selected" to the price element once it is selected.目前,一旦选择了附加产品,vue 会在选择后将“.price_selected”类添加到价格元素中。 The code above looks for ".price_selected", saves it to the variable totalPriceSelected, then prints it to the console.上面的代码查找“.price_selected”,将其保存到变量 totalPriceSelected,然后将其打印到控制台。

I am wondering how I can add all of the addon price values (once selected) to the total price in the add to cart bar.我想知道如何将所有插件价格值(一旦选择)添加到添加到购物车栏中的总价中。 I'd imagine I'd have to convert all the prices to numbers instead of strings and add them together, but I'm not quite sure how to do that.我想我必须将所有价格转换为数字而不是字符串并将它们加在一起,但我不太确定该怎么做。

Product page for reference 产品页面供参考

UPDATE更新

 watch: { selected: function(new_value) { setTimeout(function(){ var priceTotal = 0 var priceSelected = document.querySelectorAll(".price_selected"); var strippedPriceSelected = priceSelected; for(var i = 0; i < strippedPriceSelected.length; i++){ var totalPriceSelected = priceSelected[i].innerHTML; // '+$ 85.00' var priceNumber = parseFloat(totalPriceSelected.split(' ')[1]); priceTotal += priceNumber; } var currentBottomPrice = parseFloat(el2.innerHTML); el2.innerHTML = currentBottomPrice += parseFloat(priceTotal); console.log(priceTotal); }, 10); this.product.selected = new_value; //this.$emit('selected', this.product, new_value); } },

So I have this now which is adding them correctly, but when the value of priceTotal goes back to 0 (add-ons are deselected) the total stays the same because it added the value and it is tr所以我现在有这个可以正确添加它们,但是当 priceTotal 的值回到 0(取消选择附加组件)时,总数保持不变,因为它添加了值并且它是 tr

Yeah so you can split the price string into two parts on the space, then parse the second part as a float (decimal number).是的,所以您可以将价格字符串在空间上分成两部分,然后将第二部分解析为浮点数(十进制数)。 Now you can do any math you need to it.现在你可以做任何你需要的数学运算。

setTimeout(function() {    
  var priceSelected = document.querySelectorAll(".price_selected");
  var strippedPriceSelected = priceSelected;
  for(var i = 0; i < strippedPriceSelected.length; i++){
    var totalPriceSelected = priceSelected[i].innerHTML; // '+$ 85.00'
    var priceNumber = parseFloat(totalPriceSelected.split(' ')[1]);
    console.log(priceNumber) // Prints 85 as number
  }
}, 10);

Let me know if you have any questions or it doesn't work, I'd be happy to help!如果您有任何问题或它不起作用,请告诉我,我很乐意为您提供帮助!

We shouldn't just seek to have a working piece of code that solves a minor issue but could pose a major functionality break later.我们不应该仅仅寻求一个可以解决小问题但可能在以后造成主要功能中断的工作代码。 I see potential pitfalls around the approach used here:我发现这里使用的方法存在潜在的缺陷:

a) converting from string to number (using Number/parseFloat) could pose issues if the string contains invalid characters. a) 如果字符串包含无效字符,则从字符串转换为数字(使用 Number/parseFloat)可能会造成问题。 For eg, if price is from the add-on is displayed as '+$85.00' (notice the missing space after $ symbol), totalPriceSelected.split(' ')[1] will return undefined.例如,如果来自插件的价格显示为 '+$85.00'(注意 $ 符号后缺少空格),totalPriceSelected.split(' ')[1] 将返回 undefined。 Adding undefined to a number will give you NaN.将 undefined 添加到一个数字会给你 NaN。 Or, what if the add-on starts displaying the price as '85.00 $'.或者,如果加载项开始将价格显示为“85.00 美元”怎么办。 The approach used above would fail again.上面使用的方法将再次失败。

b) What if the vue component updates the CSS class for prices to something else, say amount_selected? b) 如果 vue 组件将价格的 CSS 类更新为其他内容,比如 amount_selected,该怎么办? Such a change would be beyond your control but has a direct impact on the code/logic that you would implement to display the total price.此类更改将超出您的控制范围,但会直接影响您为显示总价而实施的代码/逻辑。 Needless to say, when it comes to dealing with showing/requesting prices from users, we should be extremely careful about it.不用说,在处理向用户展示/索取价格时,我们应该非常小心。

There could be more such cases and we should look for a solution that is more robust.可能会有更多这样的情况,我们应该寻找更强大的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM