I want to add some custom billing states and then set a default state in the Admin panel. So far I have added the states as follows (code below; also not sure this is right):
add_filter( 'woocommerce_states', 'custom_woocommerce_states' );
function custom_woocommerce_states( $states ) {
$states['PE'] = array(
'PE1' => __('StateA', 'woocommerce')
'PE2' => __('StateB', 'woocommerce')
);
return $states;
}
How do I set the default value to be StateA in the Admin Panel?
2
Answers
You can add some new custom states for a country using this code:
Then you can change the country and state default value using this code:
If you are trying to make it dynamic and choose your default state from your admin panel, you can add a section and an option to your Woocommerce settings and get it using
get_option('custom_id')
. You can get help for creating a custom setting for Woocommerce here.First, there is a mistake in your current code.
In Admin WooCommerce order pages when adding (or editing) billing (or shipping) address(es), to set custom ‘PE1’ as default state when selected country is Peru (PE), use the following instead:
Code goes in functions.php file of the active child theme (or active theme). Tested and works.