I have this code in elementor custom code, it’s loaded at the end of the body tag.
<script>
jQuery(document).ready(function($){
var productPrice;
var finalProductPrice;
var shippingPrice;
var servicePrice;
var result;
var output;
//
$('#ship-price-a').on('click', function(){
if( typeof productPrice === 'undefined' ){
productPrice = $('#original-price').find('.woocommerce-Price-amount').text();
}
//
shippingPrice = $(this).attr('data-price');
if( typeof finalProductPrice === 'undefined' ){
//var vat = ( shipPrice * 22 / 100 );
finalProductPrice = ( parseFloat(productPrice) + parseFloat(shippingPrice) );
result = finalProductPrice + '€';
} else {
var p = ( parseFloat(finalProductPrice) + parseFloat(shippingPrice) );
result = p + '€';
}
output = result.replace('.', ',');
$('#final-price').text(output);
console.log('service price a click event - finalProductPrice',finalProductPrice);
});
//
$('#ship-price-b').on('click', function(){
if( typeof productPrice === 'undefined' ){
productPrice = $('#original-price').find('.woocommerce-Price-amount').text();
}
shippingPrice = $(this).attr('data-price');
//var vat = ( shipPrice * 22 / 100 );
if( typeof finalProductPrice === 'undefined' ){
finalProductPrice = ( parseInt(productPrice) + parseFloat(shippingPrice) );
result = finalProductPrice + '€';
} else {
var p = ( parseInt(finalProductPrice) + parseFloat(shippingPrice) );
result = p + '€';
}
output = result.replace('.', ',');
$('#final-price').text(output);
console.log('ship price b click event - finalProductPrice',finalProductPrice);
});
//
$('#service-price-a').on('click', function(){
servicePrice = $(this).attr('data-price');
if( typeof productPrice === 'undefined' ){
productPrice = $('#original-price').find('.woocommerce-Price-amount').text();
}
if( typeof finalProductPrice === 'undefined' ){
finalProductPrice = ( parseFloat(productPrice) + parseFloat(servicePrice) );
result = finalProductPrice + '€';
} else {
var p = ( parseFloat(finalProductPrice) + parseFloat(servicePrice) );
result = p + '€';
}
output = result.replace('.', ',');
$('#final-price').text(output);
console.log('service price a click event - finalProductPrice',finalProductPrice);
});
//
$('#service-price-b').on('click', function(){
servicePrice = $(this).attr('data-price');
if( typeof productPrice === 'undefined' ){
productPrice = $('#original-price').find('.woocommerce-Price-amount').text();
}
if( typeof finalProductPrice === 'undefined' ){
finalProductPrice = ( parseFloat(productPrice) + parseFloat(servicePrice) );
result = finalProductPrice + '€';
} else {
var p = ( parseFloat(finalProductPrice) + parseFloat(servicePrice) );
result = p + '€';
}
output = result.replace('.', ',');
$('#final-price').text(output);
console.log('service price b click event - finalProductPrice',finalProductPrice);
});
});
</script>
I’m facing an issue with it, it’s supposed to calculate the final price of a product when the user click on some additional services that can be purchased.
For the #ship-price-a
and #ship-price-b
the calculation is done correctly. When the user try to add #service-price-a
and #service-price-b
the calculation will be wrong, only one of both services will be added to the calculation. Is there something wrong into the code and where I need to modify to fix?
2
Answers
If this solution works for you, please consider upvoting the answer. Thanks, and happy coding! 😊👨💻
@ictdev Yes, you can modify the code to prevent a service from being added multiple times and allow users to switch between different service options
If this solution works for you, please consider upvoting the answer. Thanks, and happy coding! 😊👨💻