2

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?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Les
  • 330
  • 5
  • 15

2 Answers2

2

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.

enter image description here

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
-1

You can add some new custom states for a country using this code:

add_filter( 'woocommerce_states', 'custom_woocommerce_states' );
function custom_woocommerce_states( $states ) {
$states['XX'] = array( // XX is Country Code
  'XX1' => 'State 1',
  'XX2' => 'State 2'
  );
  return $states;
}

Then you can change the country and state default value using this code:

add_filter( 'default_checkout_billing_country', 'change_default_checkout_country' );
add_filter( 'default_checkout_billing_state', 'change_default_checkout_state' );
function change_default_checkout_country() {
  return 'XX'; // country code
}
function change_default_checkout_state() {
  return 'XX'; // state 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.