在Woocommerce Checkout上删除一个国家的特定州 [英] Remove specific states of a country on Woocommerce Checkout

查看:104
本文介绍了在Woocommerce Checkout上删除一个国家的特定州的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到一种方法,从WooCommerce结帐页面的州"下拉列表中删除一些美国州.

I am trying to find a way to remove a few US states from the "State" dropdown in WooCommerce checkout page.

状态列表:

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

到目前为止我一直在做什么:
我已经按照链接中的说明手动删除了它们.但这不是一个好方法,因为每次WooCommerce更新时,更改都会被覆盖.

What I have been doing so far:
I have been manually removing them as mentioned in this link. But this is not a good way, as the changes get overwritten every time WooCommerce is updated.

我找到的替代方法: 在此链接上, WooCommerce将运输限制到基于类别的州,如果满足特定条件,则有一种设置运输状态的方法. 我想知道我是否可以取消设置五个状态.与 setting 50个状态相比,这听起来更合乎逻辑.但不幸的是,我找不到任何有用的东西.

Alternative I found: On this link WooCommerce restrict shipping to states based on category, there's a way to set shipping states if specific condition is met. I am wondering if I can unset five states. This sounds more logical to me than setting 50 states. But unfortunately, I am not able to find anything useful.

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

Any ideas what could be a possible solution?

推荐答案

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

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

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

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:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆