基于WooCommerce单一产品页面中维度的价格计算 [英] Price calculation based on dimensions in WooCommerce single product page
问题描述
基于
出于某种原因,当为我的爱情选择的选项为Ingenting时,rope_price将乘以10或与0连接(它的值为0)。当我将选择的选项更改为其他任何一个时,rope_price将乘以1000或与00连接。我不知道为什么会发生这种情况。
任何想法?
因为你是连接字符串。和1+0
不一样 1 + 0
,你可以在这里查看:
console.log(1 + 0 =,1 + 0); console .log('1+0=',1+0);
从HTML对象获取值时,将其作为字符串接收。如果要将其用作数字,则必须先将其转换。您可以使用 Number
或 parseFloat
(甚至 parseInt
,但会删除小数。)
var oneNumber = 1; var oneString = 1; var oneConverted = Number(oneString); console.log(typeof oneNumber:,typeof oneNumber); console.log(typeof oneString:,typeof oneString); console.log(typeof oneConverted:,typeof oneConverted); console.log(oneNumber + oneNumber =,oneNumber + oneNumber); console.log('oneString + oneString =',oneString + oneString); console.log('oneConverted + oneConverted =',oneConverted + oneConverted) ;
您遇到的确切问题是您的 ene_enden
变量是行中的字符串var rope_price =(length * vprice)+ ene_enden;
。当您将两个字符串相乘时,它们会自动转换为数字(您的(长度* vprice)
),但是当您将该数字连接到另一个字符串时,它们会自动转换为再次输入一个字符串(你的 + ene_enden
),所以你必须先将 ene_enden
转换成一个数字,然后更好地转换所有预期的将变量编号为数字。
Based on "Get selected variation price in jQuery on Woocommerce Variable products" answer code,
in my code bellow, I have a problem with the price calculation of a WooCommerce variable product.
The price gets multiplied with 10 or 1000, depending on the option selected on a dropdown, which is not supposed to happen and I don't know why it is happening.
Here is my code:
<script>
jQuery(function($) {
var jsonData = <?php echo json_encode($variations_data); ?>,
inputVID = 'input.variation_id';
$('input').change( function(){
if( '' != $(inputVID).val() ) {
var vid = $(inputVID).val(), // VARIATION ID
length = $('#cfwc-title-field').val(), // LENGTH
diameter = $('#diameter').val(), // DIAMETER
ene_enden = $('#id_dropdown_one_end').find('option:selected').attr("value_one_end"),
vprice = ''; // Initilizing
// Loop through variation IDs / Prices pairs
$.each( jsonData, function( index, price ) {
if( index == $(inputVID).val() ) {
vprice = price; // The right variation price
}
});
var rope_price = (length*vprice) + ene_enden;
if (rope_price != 0){
$('.price').html(rope_price+',-');
}
alert('variation Id: '+vid+' || Lengde: '+length+' || Diameter: '+diameter+' || Variantpris: '+vprice+ ' || Rope price: '+rope_price+' || ene_enden = '+ene_enden);
}
});
});
</script>
For some reason rope_price gets multiplied by 10 or concatenated with 0 when the option selected for 'I enden av tauet ' is 'Ingenting'(it's value is 0). When I change the option selected to any of the others rope_price gets multiplied with 1000 or concatenated with 00. I don't know why this is happening. Any ideas?
Because you are concatenating strings. Is not the same 1 + 0
than "1" + "0"
, as you can check here:
console.log("1 + 0 =", 1 + 0);
console.log('"1" + "0" =', "1" + "0");
When you get a value from an HTML object, you receive it as a string. If you want to use it as a number you must convert it before. You can use either Number
or parseFloat
(even parseInt
, but will remove decimals).
var oneNumber = 1;
var oneString = "1";
var oneConverted = Number(oneString);
console.log("typeof oneNumber:", typeof oneNumber);
console.log("typeof oneString:", typeof oneString);
console.log("typeof oneConverted:", typeof oneConverted);
console.log("oneNumber + oneNumber =", oneNumber + oneNumber);
console.log('oneString + oneString =', oneString + oneString);
console.log('oneConverted + oneConverted =', oneConverted + oneConverted);
The exact problem you are having is your ene_enden
variable being a string in the line var rope_price = (length*vprice) + ene_enden;
. When you multiply two strings, they are automatically converted to a number (your (length*vprice)
), but when you concatenate that number to another string, they are converted automatically to a string again (your + ene_enden
), so you must first convert ene_enden
to a number, ot better convert all expected number variables to a number.
这篇关于基于WooCommerce单一产品页面中维度的价格计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!