Skip to content

Instantly share code, notes, and snippets.

@Ratne
Created April 1, 2024 15:22
Show Gist options
  • Save Ratne/a30017b472faccf75dba577cbccebaff to your computer and use it in GitHub Desktop.
Save Ratne/a30017b472faccf75dba577cbccebaff to your computer and use it in GitHub Desktop.
<php
class WFACP_Conditional_field {
private $conditional_field = [];
private $exclude_wfacp_ids=[];
public function __construct() {
$this->conditional_field = [
'select_user_type' => [
[
'value' => 'privato',
'fields' => [ 'customertaxid' ],
'enable' => true,
],
[
'value' => 'azienda',
'fields' => [ 'customervat', 'customercompany','sdi' ],
'enable' => true,
],
]
];
add_action( 'wfacp_after_checkout_page_found', [ $this, 'wfacp_add_script' ] );
add_filter( 'woocommerce_checkout_fields', [ $this, 'wfacp_remove_required' ], 9, 1 );
add_filter( 'wfacp_custom_fields', [ $this, 'wfacp_remove_field' ], 10, 1 );
add_filter( 'wfacp_forms_field', [ $this, 'conditional_country_field' ], 10, 2 );
}
function wfacp_add_script() {
if(in_array(WFACP_Common::get_id(),$this->exclude_wfacp_ids)){
return;
}
add_action( 'wp_footer', [ $this, 'wfacp_conditional_field_script' ] );
}
function wfacp_conditional_field_script() {
$fields = json_encode( $this->conditional_field );
?>
<style>
#billing_country_field b{
display:none !important;
}
p.conditional_field {
display: none !important;
}
.wfacp_dropdown option, .wfacp_dropdown select {
text-transform: capitalize;
}
</style>
<script>
jQuery(document).ready(function ($) {
var conditional_fields =<?php echo $fields;?>;
conditionField();
function conditionField() {
$.each(conditional_fields, function (field, values) {
$.each(values, function (key, value) {
var person_type= $("#" + field + "_field input[name='" + field + "']:checked").val();
displayConditionalField(field, value, person_type);
$(document.body).on('change', '#' + field + '_field input', function () {
var id = $(this).attr('id');
var val = $(this).val();
displayConditionalField(id, value, val);
});
});
});
}
$(document.body).on('change', '#billing_country', function () {
conditionField();
});
function displayConditionalField(id, values, val) {
// alert(country);
var country = $('#billing_country').val();
$.each(values.fields, function (index, field) {
var show = false;
if (val == 'null') {
val = $('#' + id + '_field input').val();
}
// console.log(field,val,values.value,values.enable);
if (val == values.value) {
var show = true;
}
// console.log(val,field,country);
if (show && values.enable) {
if('customertaxid' == field && 'CH' == country){
$('#' + field + '_field').addClass('conditional_field');
}else{
$('#' + field + '_field').removeClass('conditional_field');
}
} else {
$('#' + field + '_field').addClass('conditional_field');
}
});
}
});
</script>
<?php
}
function wfacp_remove_required( $fields ) {
if ( ! class_exists('WFACP_Common') || in_array(WFACP_Common::get_id(), $this->exclude_wfacp_ids)) {
return $fields;
}
$selected_user_type = isset($_POST['select_user_type']) ? $_POST['select_user_type'] : ''; // Otteniamo il tipo di utente selezionato
if ($selected_user_type=="privato")
{
$fields['advanced']['customertaxid']['required']=1;
$fields['advanced']['customervat']['required']='';
$fields['advanced']['customercompany']['required']='';
$fields['advanced']['sdi']['required']='';
}
if ($selected_user_type=="azienda")
{
$fields['advanced']['customertaxid']['required']='';
$fields['advanced']['customervat']['required']=1;
$fields['advanced']['customercompany']['required']=1;
$fields['advanced']['sdi']['required']=1;
}
return $fields;
}
function wfacp_remove_field( $fields ) {
if(in_array(WFACP_Common::get_id(),$this->exclude_wfacp_ids)){
return $fields;
}
foreach ( $this->conditional_field as $field => $conditional_fields ) {
foreach ( $conditional_fields as $conditional_field ) {
if ( ! isset( $_REQUEST[ $field ] ) || ( false == $conditional_field['enable'] || $_REQUEST[ $field ] == $conditional_field['value'] ) ) {
continue;
}
foreach ( $conditional_field['fields'] as $field_id ) {
if ( ! empty( $fields['advanced'][ $field_id ] ) ) {
unset( $fields['advanced'][ $field_id ] );
}
}
}
}
//WFACP_Common::pr($fields['billing']);
return $fields;
}
function conditional_country_field( $field, $key ) {
if(in_array(WFACP_Common::get_id(),$this->exclude_wfacp_ids)){
return $field;
}
if ( $key == 'billing_country' ) {
$countries_obj = new WC_Countries();
$countries = $countries_obj->__get( 'countries' );
$field['type'] = 'select';
$field['input_class'][] = 'country_to_state country_select';
$field['options'] = $countries;
}
return $field;
}
}
new WFACP_Conditional_field();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment