Remove specific states of a country on Woocommerce Checkout

百般思念 提交于 2019-12-02 06:32:23

The simplest way is to remove them them using the dedicated filter hook woocommerce_states this way:

add_filter( 'woocommerce_states', 'custom_us_states', 10, 1 );
function custom_us_states( $states ) {
    $non_allowed_us_states = array( 'AK', 'HI', 'AA', 'AE', 'AP'); 

    // Loop through your non allowed us states and remove them
    foreach( $non_allowed_us_states as $state_code ) {
        if( isset($states['US'][$state_code]) )
            unset( $states['US'][$state_code] );
    }
    return $states;
}

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

Then you can add some condition or make a custom settings page like in the threads below:

Best way of doing this would be using a filter, add this snippet in your themes functions.php

/**
 * Modify Woocommerce states array
 *
 * @param array $states, collection of all $states
 * @return array $states, modified array.
*/
add_filter( 'woocommerce_states', function( $states ){
    // Unset Hawaii
    unset( $states['US']['HI'] );

    return $states;
}, 999);

You can unset any states like this.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!