Created
July 16, 2025 06:44
-
-
Save xlplugins/fd2e69130fc605acfa27cfd3dda8b69c to your computer and use it in GitHub Desktop.
show conditional field when Shipping country is....
This file contains hidden or 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
class Temp_WFACP_Conditional_field { | |
private $conditional_field = []; | |
public function __construct() { | |
$this->conditional_field = [ | |
'shipping_country' => [ | |
[ | |
'value' => 'IE', | |
'fields' => [ 'custom_text' ], | |
'enable' => true, | |
'type' => 'advanced', | |
'condition' => [ | |
'IE', | |
], | |
] | |
] | |
]; | |
add_action( 'wfacp_internal_css', [ $this, 'scripts' ] ); | |
add_filter( 'woocommerce_checkout_fields', [ $this, 'wfacp_remove_required' ], 10, 1 ); | |
} | |
public function scripts() { | |
$fields = json_encode( $this->conditional_field ); | |
?> | |
<style> | |
div.conditional_field, | |
p.conditional_field { | |
display: none !important; | |
} | |
.wfacp_dropdown option, | |
.wfacp_dropdown select { | |
text-transform: none; | |
} | |
</style> | |
<script> | |
jQuery(document).ready(function ($) { | |
var conditional_fields =<?php echo $fields;?>; | |
each_field(); | |
wfacp_frontend.hooks.addFilter('wfacp_google_map_address_data', function (data) { | |
setTimeout(function () { | |
each_field(); | |
}, 1000); | |
return data; | |
}); | |
function each_field() { | |
$.each(conditional_fields, function (field, values) { | |
$.each(values, function (key, value) { | |
displayConditionalField(field, value, 'null'); | |
$(document.body).on('change', '#' + field, function () { | |
var id = $(this).attr('id'); | |
var val = $(this).val(); | |
if ($(this).parent('checkbox')) { | |
if ($(this).prop("checked")) { | |
val = 'checked'; | |
} | |
} | |
displayConditionalField(id, value, val); | |
}); | |
}); | |
}); | |
} | |
function check_value_in_array(arr, obj) { | |
for (var i = 0; i < arr.length; i++) { | |
if (arr[i] == obj) { | |
return true; | |
} | |
} | |
return false; | |
} | |
function displayConditionalField(id, values, val) { | |
$.each(values.fields, function (index, field) { | |
var show = false; | |
if (val == 'null') { | |
val = $('#' + id).val(); | |
} | |
var condition_to_display = values.condition; | |
if (typeof condition_to_display === 'object' && Object.keys(condition_to_display).length > 0 && check_value_in_array(condition_to_display, val) === true) { | |
show = true; | |
} else if (val == values.value) { | |
show = true; | |
} | |
if (show && values.enable) { | |
$('.' + field).removeClass('conditional_field'); | |
$('#' + field + '_field').removeClass('conditional_field'); | |
} else { | |
$('#' + field).val(''); | |
$('.' + field).addClass('conditional_field'); | |
$('#' + field + '_field').addClass('conditional_field'); | |
} | |
}); | |
} | |
}); | |
</script> | |
<?php | |
} | |
function wfacp_remove_required( $fields ) { | |
foreach ( $this->conditional_field as $field => $conditional_fields ) { | |
foreach ( $conditional_fields as $conditional_field ) { | |
if ( 'checked' == $conditional_field['value'] ) { | |
$conditional_field['value'] = 1; | |
} | |
if ( isset( $_REQUEST[ $field ] ) && ( false == $conditional_field['enable'] || $_REQUEST[ $field ] == $conditional_field['value'] ) ) { | |
continue; | |
} | |
if ( is_array( $conditional_field['condition'] ) && count( $conditional_field['condition'] ) > 0 && isset($_REQUEST[ $field ]) && in_array( $_REQUEST[ $field ], $conditional_field['condition'] ) ) { | |
continue; | |
} | |
foreach ( $conditional_field['fields'] as $field_id ) { | |
$section = ''; | |
if ( isset( $fields['billing'][ $field_id ] ) ) { | |
$section = 'billing'; | |
} elseif ( isset( $fields['shipping'][ $field_id ] ) ) { | |
$section = 'shipping'; | |
} elseif ( isset( $fields['advanced'][ $field_id ] ) ) { | |
$section = 'advanced'; | |
}; | |
if ( ! empty( $section ) && ! isset( $_REQUEST[ $field ] ) ) { | |
unset( $fields[ $section ][ $field_id ]['required'] ); | |
} else if ( isset( $_REQUEST[ $field ] ) && ! in_array( $_REQUEST[ $field ], $conditional_field['condition'] ) ) { | |
unset( $fields[ $section ][ $field_id ]['required'] ); | |
} | |
} | |
} | |
} | |
return $fields; | |
} | |
} | |
new Temp_WFACP_Conditional_field(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment