Last active
August 23, 2020 09:20
-
-
Save wpmudev-sls/533a3b214dba9d1d9056d47c8354c405 to your computer and use it in GitHub Desktop.
[Forminator Pro] - Add currency formatting to Calculation Fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [Forminator Pro] - Add currency formatting | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Adds currency formatting in calculation fields | |
* Author: Maciek Palmowski & Panos Lyrakis @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
add_action( 'wp_footer', function() { | |
global $post; | |
if ( ! $post instanceof WP_Post || ! has_shortcode( $post->post_content, 'forminator_form' ) ) { | |
return; | |
} | |
?> | |
<script type="text/javascript"> | |
(($,d)=>{ | |
if ( window.wpmudev_forminator_fixed_calculation_digits ) { | |
return; | |
} | |
window.wpmudev_forminator_fixed_calculation_digits = { | |
calculation_class : 'forminator-calculation', | |
holder_fields_prefix : 'field_holder', | |
cform_ajax_action : 'forminator_load_cform', | |
preparation_done : false, | |
formatter : new Intl.NumberFormat('en-GB', { | |
style: 'currency', | |
currency: 'GBP', | |
}), | |
run: function() { | |
// added custom country format | |
if( window.wpmudev_country_format ){ | |
this.formatter = window.wpmudev_country_format; | |
} | |
// 1st hidde th calculation fields add a new text field | |
// that will hold their value with the currencty format | |
//this.prepare_fields(); | |
$(d).ajaxComplete( function( event, xhr, settings ) { | |
let action = wpmudev_forminator_fixed_calculation_digits.get_query_var( 'action', settings.data ); | |
if ( action === wpmudev_forminator_fixed_calculation_digits.cform_ajax_action ) { | |
//setTimeout(function() { | |
wpmudev_forminator_fixed_calculation_digits.prepare_fields(); | |
//}, 100); | |
} | |
}); | |
$( `.${this.calculation_class}` ).on( 'change', function(){ | |
if ( ! wpmudev_forminator_fixed_calculation_digits.preparation_done ) { | |
return; | |
} | |
// Update the field holder value with formated value | |
let field = $( this ), | |
field_holder = field.parent().find( `#${wpmudev_forminator_fixed_calculation_digits.holder_fields_prefix}-${field.attr("id")}` ); | |
wpmudev_forminator_fixed_calculation_digits.set_field_val( field_holder, field.val() ); | |
} ); | |
}, | |
prepare_fields: function() { | |
if ( wpmudev_forminator_fixed_calculation_digits.preparation_done ) { | |
return; | |
} | |
wpmudev_forminator_fixed_calculation_digits.preparation_done = true; | |
$( `.${this.calculation_class}` ).each( function(){ | |
let field = $(this), | |
field_holder = $( '<input />',{ | |
type : "text", | |
id : `${wpmudev_forminator_fixed_calculation_digits.holder_fields_prefix}-${field.attr( "id" )}`, | |
"class" : field.attr( 'class' ), | |
"disabled" : "disabled" | |
} ); | |
wpmudev_forminator_fixed_calculation_digits.set_field_val( field_holder, field.val() ); | |
field.css( 'display', 'none' ); | |
field.after( field_holder ); | |
}); | |
}, | |
set_field_val: function( field, value ){ | |
let formated_value = wpmudev_forminator_fixed_calculation_digits.formatter.format( value ); | |
field.val( formated_value ); | |
}, | |
get_query_var: function( name, url ) { | |
if ( ! url ) { | |
return false; | |
} | |
name = name.replace(/[\[\]]/g, "\\$&"); | |
var regex = new RegExp(name + "(=([^&#]*)|&|#|$)"), | |
results = regex.exec(url); | |
if (!results) return false; | |
if (!results[2]) return false; | |
return decodeURIComponent( results[2].replace(/\+/g, " ") ); | |
} | |
}; | |
$(d).ready( function(){ | |
$(document).on( 'after.load.forminator',function( e, form_id ) { | |
wpmudev_forminator_fixed_calculation_digits.run(); | |
}); | |
} ); | |
})(jQuery,document); | |
</script> | |
<?php | |
}, 21); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment