duanbin4847 2016-09-26 08:59
浏览 44
已采纳

将城镇/城市文本字段更改为结帐表单中的选择选项字段

In Woocommerce, I would like to change Town/City text field in a select field option field.

What should i do?

Here is a screenshot:

enter image description here

Thanks

  • 写回答

2条回答 默认 最新

  • doujiao1949 2016-09-26 09:56
    关注

    You need first to change the field type from 'text' to 'select' using the dedicated hook woocommerce_default_address_fields. Then you have also to change the label and to and an options argument where you are going to set your towns in an array of key/values.

    In this array, you will have a line by town separated by a coma.

    Here is the code:

    add_filter( 'woocommerce_default_address_fields' , 'customize_checkout_city_field' );
    function customize_checkout_city_field( $address_fields ) {
    
        // Set HERE the cities (one line by city)
        $towns_cities_arr = array(
            '0' => __('Select your city', 'my_theme_slug'),
            'paris' => 'Paris',
            'versailles' => 'Versailles',
            'cannes' => 'Cannes',
        );
    
        // Customizing 'billing_city' field
        $address_fields['city']['type'] = 'select';
        $address_fields['city']['class'] = array('form-row-last', 'my-custom-class'); // your class here
        $address_fields['city']['label'] = __('Town / city', 'my_theme_slug');
        $address_fields['city']['options'] = $towns_cities_arr;
    
    
        // Returning Checkout customized fields
        return $address_fields;
    
    }
    

    This code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    The code is tested and fully functional.


    Update: To add your custom class, replace in $address_fields['city']['class']… the class 'my-custom-class' by yours.


    References:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条