在Woocommerce Checkout上删除一个国家的特定州 [英] Remove specific states of a country on Woocommerce Checkout
问题描述
我正在尝试找到一种方法,从WooCommerce结帐页面的州"下拉列表中删除一些美国州.
I am trying to find a way to remove a few US states from the "State" dropdown in WooCommerce checkout page.
状态列表:
- 夏威夷
- 阿拉斯加
- 武装部队(AA)
- 武装部队(AE)
- 武装部队(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:
- 禁用某些状态Woocommerce中购物车中是否有特定产品
- 美国通过Woocommerce自定义设置页面允许的国家
- Disable some states if specific products are in cart in Woocommerce
- US allowed States through Woocommerce custom setting page
这篇关于在Woocommerce Checkout上删除一个国家的特定州的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!