John Davidson

php - Populate Additional Fields Based On Select Value WooCommerce

0 comments
Message:


So my site has a custom field implemented with a drop down of suburbs , upon clicking how do I make the City and the Post code related to that item.
Example: I select Bat Cave from the drop down, it would populated Post with 0101 and City with Gothem


add_filter('woocommerce_billing_fields', 'custom_woocommerce_billing_fields');
function custom_woocommerce_billing_fields($fields) {
$fields['billing_suburb'] = array(
'label' => __('Your Suburb', 'woocommerce'), // Add custom field label
'placeholder' => _x('Your Suburb', 'placeholder', 'woocommerce'), // Add custom field placeholder
'required' => false, // if field is required or not
'clear' => false, // add clear or not
'type' => 'select',
'options' => array(
'' => 'Please select',
'ny' => 'Nester',

How do I tie values into those selects so that they can populate the additional fields.


Any help greatly appreciated.



My Answer:
> 'Suburb',
> 'required' => true,
> 'class' => array('form-row-wide'),
> 'clear' => true,
> 'type' => 'select',
> 'options' => array(
> 'Bat Cave' => 'Bat Cave',
> 'Gothem' => 'Gothem',
> 'Metropolis' => 'Metropolis'
> )
> );

$fields['billing_city'] = array(
'label' => 'City',
'required' => true,
'class' => array('form-row-wide'),
'clear' => true,
'type' => 'text',
'placeholder' => 'Enter your city'
);

$fields['billing_postcode'] = array(
'label' => 'Postcode',
'required' => true,
'class' => array('form-row-wide'),
'clear' => true,
'type' => 'text',
'placeholder' => 'Enter your postcode'
);

return $fields;
}

// Add custom jQuery script to populate city and postcode based on selected suburb
add_action('wp_footer', 'custom_populate_city_postcode_script');
function custom_populate_city_postcode_script() {
?>

}

This code adds custom fields for suburb, city, and postcode in the WooCommerce billing fields and also adds a jQuery script to populate the city and postcode based on the selected suburb from the dropdown. You can modify the script to add more suburbs and their corresponding city and postcode values.

Rate this post

3 of 5 based on 1566 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us