1

On shipping to the US I am trying to restrict the shipping address to one state (Kentucky), the code below works on WooCommerce 3.5.8, but as soon as update WooCommerce it stops working. I have tried the different increments (3.6.0 beta, rc1, etc) and everything after 3.5.8 causes it to stop working (it is showing all of the states instead of just Kentucky).

I have tried searching to see if that option has changed or been deprecated but I wasn't able to find it or another way of doing it. I have tried some other code I found here too but that didn't work either.

 add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );

function custom_override_checkout_fields( $fields ) 
{
    $fields['shipping']['shipping_state']['options'] = array('KY' => 'Kentucky' );

    return $fields;
}

Is there an issue with the above or some change I need to make?

1 Answer 1

2

The following will set a default pre-defined shipping country and state, displaying the shipping state select field as disabled (read only) with "Kentucky" selected value:

add_filter( 'woocommerce_shipping_fields' , 'custom_shipping_state_fields', 100, 1 );
function custom_shipping_state_fields( $fields ) {
    WC()->customer->set_shipping_country('US'); // Set shipping country
    WC()->customer->set_shipping_state('KY'); // Set shipping state

    $fields['shipping_state']['type'] = 'select';
    $fields['shipping_state']['options'] = ['KY' => __('Kentucky', 'WooCommerce')];
    $fields['shipping_state']['default'] = 'KY';
    $fields['shipping_state']['input_class'] = [];
    $fields['shipping_state']['custom_attributes'] = ['disabled' => 'disabled'];

    return $fields;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and work.

4
  • That does appear to work, but if I remove the disabled option I can see all of the other states. In 3.5.8 the only state that would appear is Kentucky.
    – Howli
    May 27, 2019 at 19:17
  • @Howli Is not possible to remove sates only on shipping on last WooCommerce version… This only can be done with some other hook on both billing and shipping. Now the important thing is that the shipping state is always Kentucky, what this code does. May 27, 2019 at 19:17
  • I just tested that code with 3.5.8 and it did remove all the states in the dropdown apart from Kentucky. It was possible, but now it isn't
    – Howli
    May 27, 2019 at 19:23
  • @Howli Not anymore since WooCommerce 3.6+ May 27, 2019 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.