Hi, thanks for the reply.
I am trying to embed WooCommerce basic fields like Country, State, City/Town, Zip and Phone into user Settings page as required fields. When I am saving the form as I am facing the field Validation error, and I’ve tried to browse around to see if it could be resolved.
Here is my snippet, but solution is incomplete. The issue coming from State field ‘billing_state’.
<?php
add_filter(
'hivepress/v1/forms/user_update',
function( $form ) {
$form['fields']['first_name']['required'] = true;
$form['fields']['last_name']['required'] = true;
// Add billing fields
$form['fields']['billing_country'] = array(
'type' => 'select',
'label' => __( 'Country', 'your-text-domain' ),
'required' => true,
'options' => WC()->countries->get_allowed_countries(), // Get allowed countries
'class' => array('select2'), // Add class for Select2
);
$form['fields']['billing_state'] = array(
'type' => 'select',
'source' => admin_url( 'admin-ajax.php' ),
'label' => __( 'State', 'your-text-domain' ),
'required' => false,
'options' => [], // To be populated dynamically based on country
'class' => array('select2'), // Add class for Select2
'attributes' => [
'data-parent' => 'billing_country',
'data-options' => wp_json_encode(
[
'action' => 'get_states'
]
)
]
);
$form['fields']['billing_city'] = array(
'type' => 'text',
'label' => __( 'City/Town', 'your-text-domain' ),
'required' => true,
);
$form['fields']['billing_zip'] = array(
'type' => 'text',
'label' => __( 'Zip/Postal Code', 'your-text-domain' ),
'required' => true,
);
$form['fields']['billing_phone'] = array(
'type' => 'text',
'label' => __( 'Phone', 'your-text-domain' ),
'required' => true,
);
return $form;
},
1000
);
// Enqueue JavaScript to load states based on selected country
add_action( 'wp_enqueue_scripts', function() {
if ( is_user_logged_in() ) {
wp_enqueue_script( 'load-states', get_stylesheet_directory_uri() . '/js/hivepress/hivepress-state-country-woocommerce-ajax.js', array('jquery'), null, true );
wp_localize_script( 'load-states', 'ajax_object', array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'states' => WC()->countries->get_states(),
));
}
});
// AJAX to retrieve states based on selected country
add_action( 'wp_ajax_get_states', 'get_states' );
add_action( 'wp_ajax_nopriv_get_states', 'get_states' );
function get_states() {
$country = $_POST['parent_value'] ?? 'UA';
$states = WC()->countries->get_states( $country );
wp_send_json( $states );
}
add_filter('hivepress/v1/models/user', function ($model) {
$model['fields']['billing_first_name'] = [
'type' => 'text',
'_external' => true,
];
$model['fields']['billing_last_name'] = [
'type' => 'text',
'_external' => true,
];
$model['fields']['billing_city'] = [
'type' => 'text',
'_external' => true,
];
$model['fields']['billing_country'] = [
'type' => 'text',
'_external' => true,
];
$model['fields']['billing_state'] = [
'type' => 'text',
'_external' => true,
];
$model['fields']['billing_zip'] = [
'type' => 'text',
'_external' => true,
];
$model['fields']['billing_phone'] = [
'type' => 'number',
'_external' => true,
];
return $model;
});
Here is my JS that handle automatic load of Country/State:
jQuery(document).ready(function($) {
$('select[name="billing_country"]').change(function() {
var country = $(this).val();
console.log(country)
$.ajax({
type: 'POST',
url: ajax_object.ajax_url,
data: {
action: 'get_states',
country: country,
},
success: function(response) {
var $stateField = $('select[name="billing_state"]');
$stateField.empty(); // Clear existing options
if (response) {
$.each(response, function(key, value) {
$stateField.append($('<option></option>').attr('value', key).text(value));
});
}
}
});
});
});
Thanks for looking into this.