【问题标题】:Remove specific states of a country on Woocommerce Checkout在 Woocommerce Checkout 上删除某个国家/地区的特定州
【发布时间】:2019-05-09 17:37:58
【问题描述】:

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

州列表:

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

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

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

有什么想法可以解决吗?

【问题讨论】:

    标签: php wordpress woocommerce state countries


    【解决方案1】:

    最简单的方法是使用专用过滤器挂钩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 文件中。经过测试和工作。

    然后您可以添加一些条件或制作自定义设置页面,如下面的主题:

    【讨论】:

      【解决方案2】:

      最好的方法是使用过滤器,在你的主题functions.php中添加这个sn-p

      /**
       * 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);
      

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

      【讨论】:

      • 一些附加信息,prio 999 可能不需要,但这确保您也可以过滤插件添加的任何数据。如果您不确定要使用哪些键值来取消设置其他状态,请在源文件中查找它们:github.com/woocommerce/woocommerce/blob/master/i18n/states/…
      • 谢谢,这也有效。我接受另一篇文章作为答案的唯一原因是它包含所有州代码。
      猜你喜欢
      • 2015-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-03
      • 2019-03-09
      • 2021-07-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多