简体   繁体   English

自动更正为最接近的可能值(数量形式WordPress)

[英]Autocorrect to closest possible value (quantity form WordPress)

I am building a webshop with WooCommerce. 我正在用WooCommerce建立一个网上商店。 We are using WooCommerce advanced quantity so the quantity is not like 1,2,3,4 etc but in steps of 0.72 for example. 我们使用的是WooCommerce高级数量,因此数量不是像1,2,3,4等,而是以0.72为步长。 When using the +/- button it is working fine. 使用+/-按钮时,它工作正常。 (steps: 0.72, 1.44, 2.16 etc) (步骤:0.72、1.44、2.16等)

When I fill in 20 with my keyboard and click add to my cart in my cart there has been add a quantity of 19.44 instead of 20 because 20 is not an possible option. 当我用键盘填写20并单击“添加到购物车中的购物车”时,添加的数量是19.44而不是20,因为不可能使用20。 I want the field to autocorrect to the closest possible quantity value before adding it to my cart. 我希望该字段在将其添加到购物车之前自动校正为最接近的数量值。 So more like autocorrect it after deselecting the field. 因此,更像是取消选择字段后自动更正它。

How can I achieve this? 我该如何实现? Has it something to do with javascript? 与javascript有关吗? Hope you guys can help me! 希望你们能帮助我!

Link to example page so you can test it: https://www.stenenentegels.nl/product/trommelsteen-chelsea-20x30x4/ 链接到示例页面,以便您可以对其进行测试: https : //www.stenenentegels.nl/product/trommelsteen-chelsea-20x30x4/

You could do it in the blur event of the #qty field with javascript: 您可以使用javascript在#qty字段的blur事件中执行此操作:

jQuery("#qty").on("blur", function(){
    correctValue = jQuery(this).val() - (jQuery(this).val() % 0.72);   
    jQuery(this).val(correctValue.toFixed(2));
})

Edit: You need to find a place to put this - in a WooCommerce template or in the general js file of the site. 编辑:您需要找到放置它的位置-WooCommerce模板或网站的常规js文件中。

Edit 2: You can check that this actually works by pasting the above code in the Chrome JS console and then entering "20" in the field and clicking out of the field again. 编辑2:您可以通过将上面的代码粘贴到Chrome JS控制台中,然后在字段中输入“ 20”,然后再次单击该字段,来检查它是否确实有效。

You can just set the value to the current value minus itself with the modulus operator. 您可以使用模数运算符将值设置为当前值减去自身值。 You can determine the best event, but blur would probably work best. 您可以确定最佳事件,但是blur可能最好。

 let qty = document.getElementById('qty'); qty.onblur = function(){ let val = this.value; // Current Value let step = this.getAttribute('step'); // Get step instead of hard coding it this.value = (val - (val % step)).toFixed(2); // Simple math that adjusts the value on blur } 
 <input type="number" id="qty" step=".72" /> 

To round up one step, you could consider: 要向上舍入一个步骤,您可以考虑:

 let qty = document.getElementById('qty'); qty.onblur = function(){ let val = this.value; // Current Value let step = this.getAttribute('step'); // Get step instead of hard coding it let roundDown = (val - (val % step)).toFixed(2); let roundUp = (parseFloat(roundDown) + parseFloat(step)).toFixed(2); this.value = roundUp; } 
 <input type="number" id="qty" step=".72" /> 

If you only want this to effect steps that aren't 1 , just wrap that in an if statement: 如果只希望它影响不是1步骤,则将其包装在if语句中:

 let qty = document.getElementById('qty'); qty.onblur = function(){ let val = this.value; // Current Value let step = this.getAttribute('step'); // Get step instead of hard coding it if( step != 1 ){ let roundDown = (val - (val % step)).toFixed(2); let roundUp = (parseFloat(roundDown) + parseFloat(step)).toFixed(2); this.value = roundUp; } } 
 <input type="number" id="qty" step=".72" /> 

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

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