在Woocommerce Checkout上删除国家的特定州

时间:2018-12-08 00:01:54

标签: php wordpress woocommerce state countries

我正试图找到一种方法,从WooCommerce结帐页面的“州”下拉列表中删除一些美国州。

状态列表:

  1. 夏威夷
  2. 阿拉斯加
  3. 武装部队(AA)
  4. 武装部队(AE)
  5. 武装部队(AP)

到目前为止我一直在做什么:
我一直在手动删除它们,如本link中所述。但这不是一个好方法,因为每次WooCommerce更新时,更改都会被覆盖。

我找到的替代方法: 在此链接WooCommerce restrict shipping to states based on category上,有一种方法可以在满足特定条件的情况下设置运输状态。 我想知道是否可以取消设置五个状态。与 setting 50个状态相比,这听起来更合乎逻辑。但不幸的是,我找不到任何有用的东西。

有什么想法是可能的解决方案?

2 个答案:

答案 0 :(得分:3)

最简单的方法是使用专用过滤器钩子woocommerce_states通过以下方式将它们删除:

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;
}

代码进入活动子主题(或活动主题)的functions.php文件中。经过测试和工作。

  

然后,您可以添加条件或进行自定义设置页面,如以下线程所示:

答案 1 :(得分:1)

最好的方法是使用过滤器,将此主题添加到主题函数中。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);

您可以取消设置任何这样的状态。