Skip to content

Instantly share code, notes, and snippets.

@geekontheroad
Last active August 18, 2023 13:39
Show Gist options
  • Save geekontheroad/72e1fab9b20ce560286a0564a06459f5 to your computer and use it in GitHub Desktop.
Save geekontheroad/72e1fab9b20ce560286a0564a06459f5 to your computer and use it in GitHub Desktop.
Hide the description of the EU VAT field when outside of EU
<?php
/**
* This snippet is meant for the EU VAT for Gravity Forms addon
* It will hide the EU VAT field description when a country outside of Europe is selected
*
* INSTRUCTIONS
* Copy to your functions.php or code snippet plugin. Dont copy the starting <?php tags
*
* @author Johan d'Hollander
* @link https://geekontheroad.com
* @version 1.0
**/
add_action( 'gform_register_init_scripts', 'gotrgf_hide_field_description', 100, 2 );
function gotrgf_hide_field_description( $form , $field_values ) {
$tax_fields = GFAPI::get_fields_by_type( $form, array( 'gotrgf_eu_vat_field' ) ); //there can only be one here
if ( !empty( $tax_fields ) ) {
$tax_field = $tax_fields[0]; //get the field object
$address_field_id = $tax_field['EuVatAddress']; //get linked address field id
$euvatworldwide = $tax_field['EuVatWorldWide'] != '' ? $tax_field['EuVatWorldWide'] : 'false';
if(is_numeric($address_field_id)) {
$script = '(function($){' .
'jQuery(document).on("click change", "#input_'.$form['id'].'_'.$address_field_id.'_6", function(event) { ' .
'let abbr = jQuery(this).find(":selected").data("abbr"); ' .
'let vat = jQuery(this).find(":selected").data("vat"); ' .
'let formId = jQuery(this).find(":selected").data("formid"); ' .
'let EuVatWorldWide = ' . $euvatworldwide . ';' .
'let description = jQuery("#gfield_description_'.$form['id'].'_'.$tax_field['id'].'") ;' .
'if(abbr && vat && formId && EuVatWorldWide != "false") {' .
'description.show();' .
'} else { ' .
'description.hide();' .
'}'.
'});' .
'})(jQuery);';
GFFormDisplay::add_init_script( $form['id'], 'gotrgf_hide_field_description', GFFormDisplay::ON_PAGE_RENDER, $script );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment