基于对我的最后一个问题的回答
所有代码均有效,但窗口警报消息上方的部分除外。我也尝试在警报消息上方的php标签内添加过滤器功能get_price_html()
,但它会显示所有类型的错误。
是否可以仅使用javascript更改显示的价格?
这是我实际使用的代码:
add_action( 'woocommerce_before_add_to_cart_quantity', 'func_option_valgt');
function func_option_valgt() {
global $product;
if ( $product->is_type('variable') ) {
$variations_data =[]; // Initializing
// Loop through variations data
foreach($product->get_available_variations() as $variation ) {
// Set for each variation ID the corresponding price in the data array (to be used in jQuery)
$variations_data[$variation['variation_id']] = $variation['display_price'];
}
?>
<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
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;
document.cookie = 'rope_price_cookie='+rope_price;
////////// This is where I would like to add some code to change the displayed price //////
$('price') = rope_price; /// something like this, only it doesn't work :(
alert('variation Id: '+vid+' || Length: '+length+' || Diameter: '+diameter+' || Variantprice: '+vprice+' ||Rope price: '+rope_price);
}
}
});
});
</script>
<?php
}
}
任何帮助表示赞赏。
答案 0 :(得分:1)
您可以为<span>
元素(例如<span id="price"></span>
)分配ID,然后在JS中执行以下操作:
$("#price").html(rope_price);
请注意,#
表示一个ID,也可以使用类选择器(.price
),如果您在页面的多个部分中显示价格,该选择器将很有用。然后,您可以在使用类时一次更新所有这些元素(使其成为更具描述性的类,然后是price
,然后是防止名称冲突)