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:
add_filter( 'woocommerce_states', 'custom_woocommerce_states_for_peru' );
function custom_woocommerce_states_for_peru( $states ) {
// For PERU
$states['PE'] = array(
'PE1' => __('StateA', 'woocommerce'),
'PE2' => __('StateB', 'woocommerce')
);
return $states;
}
// Admin orders: Set a default state for PERU country
add_action( 'admin_footer', 'custom_admin_shop_order_js' );
function custom_admin_shop_order_js() {
global $pagenow, $post_type;
if ( in_array( $pagenow, array('post-new.php', 'post.php') ) && 'shop_order' === $post_type ) :
?><script type='text/javascript'>
jQuery( function($) {
// Billing state
$(document.body).on( 'change', 'select#_billing_country,select#_shipping_country', function(){
var country = 'PE', // Set country
defaultState = 'PE1', // Set default state (for country)
parent = $(this).parent().parent(),
billingState = parent.find('select#_billing_state'),
shippingState = parent.find('select#_shipping_state');
if( country === $(this).val() ) {
if ( '' === billingState.val() ) {
billingState.val(defaultState).trigger("change");
} else if ( '' === shippingState.val() ) {
shippingState.val(defaultState).trigger("change");
}
}
});
});
</script><?php
endif;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
