Last active
July 17, 2025 10:12
-
-
Save xlplugins/ad5e28337aede1380fb88d25a3d642c8 to your computer and use it in GitHub Desktop.
display state field when not available in checkout page
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 Custom_State_Field_Handler { | |
public function __construct() { | |
add_filter( 'wfacp_form_section', [ $this, 'inject_state_into_form_section' ], 10 ); | |
add_filter( 'wfacp_checkout_fields', [ $this, 'inject_state_into_checkout_fields' ], 10 ); | |
} | |
/** | |
* Injects state field into form section after country field if missing. | |
*/ | |
public function inject_state_into_form_section( $section ) { | |
if ( ! empty( $section['fields'] ) && is_array( $section['fields'] ) ) { | |
$country_field_ids = [ 'billing_country', 'shipping_country' ]; | |
$state_field_ids = [ 'billing_state', 'shipping_state' ]; | |
$existing_field_ids = wp_list_pluck( $section['fields'], 'id' ); | |
$new_fields = []; | |
foreach ( $section['fields'] as $field ) { | |
$new_fields[] = $field; | |
if ( isset( $field['id'] ) && in_array( $field['id'], $country_field_ids ) ) { | |
$country_id = $field['id']; | |
$index = array_search( $country_id, $country_field_ids, true ); | |
$state_id = $state_field_ids[ $index ]; | |
if ( ! in_array( $state_id, $existing_field_ids, true ) ) { | |
$state_field = $this->get_state_field_array( $state_id, $country_id, $field['class'], $field['field_type'] ); | |
$new_fields[] = $state_field; | |
} | |
} | |
} | |
$section['fields'] = $new_fields; | |
} | |
return $section; | |
} | |
/** | |
* Injects state field into checkout fields array when wp_doing_ajax(). | |
*/ | |
public function inject_state_into_checkout_fields( $fields ) { | |
if ( wp_doing_ajax() ) { | |
$country_field_ids = [ 'billing_country', 'shipping_country' ]; | |
$state_field_ids = [ 'billing_state', 'shipping_state' ]; | |
foreach ( $country_field_ids as $index => $country_id ) { | |
$state_id = $state_field_ids[ $index ]; | |
foreach ( [ 'billing', 'shipping' ] as $group ) { | |
if ( isset( $fields[ $group ] ) ) { | |
if ( isset( $fields[ $group ][ $country_id ] ) && ! isset( $fields[ $group ][ $state_id ] ) ) { | |
$class = isset( $fields[ $group ][ $country_id ]['class'] ) ? $fields[ $group ][ $country_id ]['class'] : [ 'form-row-wide' ]; | |
$field_type = $group; | |
$fields[ $group ][ $state_id ] = $this->get_state_field_array( $state_id, $country_id, $class, $field_type ); | |
} | |
} | |
} | |
} | |
} | |
return $fields; | |
} | |
/** | |
* Returns a consistent state field array. | |
*/ | |
private function get_state_field_array( $state_id, $country_id, $class, $field_type ) { | |
return [ | |
'type' => 'state', | |
'label' => 'State', | |
'required' => false, | |
'class' => $class, | |
'priority' => 80, | |
'country_field' => $country_id, | |
'cssready' => [], | |
'field_type' => $field_type, | |
'placeholder' => '', | |
'id' => $state_id, | |
'address_group' => 1, | |
]; | |
} | |
} | |
// Initialize | |
new Custom_State_Field_Handler(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment