首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将WooCommerce城市字段更改为前端和管理中的下拉列表

将WooCommerce城市字段更改为前端和管理中的下拉列表
EN

Stack Overflow用户
提问于 2020-09-27 10:27:21
回答 1查看 1.3K关注 0票数 2

我已经把城市区域作为下拉列表添加了,但是我不能让它出现在后端。https://ibb.co/3rdsQpY

代码语言:javascript
复制
/* city dropdown */

// UPDATE CHECKOUT CITY FIELD
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
add_filter('woocommerce_admin_shipping_fields', 'custom_override_default_address_fields');// manual order


function custom_override_default_address_fields( $address_fields ) {

    // Billing City
    $address_fields['city']['type'] = 'select';
    $address_fields['city']['label'] = __('City', 'custom-domain');
    $address_fields['city']['priority'] = '41';
    $address_fields['city']['class'] = array('my-field-class form-row-last');
    $address_fields['city']['options'] = array(
       // '' => __( 'Select unit type' ),    /*= this will make empty field*/
        'Jeddah' => __('Jeddah', 'custom-domain'),
    );

    // Sort
    ksort($address_fields['city']['options']);


    return $address_fields;
}

我试图在下面添加过滤器,但它没有工作。我遗漏了什么?

代码语言:javascript
复制
add_filter( 'woocommerce_customer_meta_fields', 'custom_override_default_address_fields' );
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-27 16:41:10

下面的内容将将计费和航运城市字段更改为

  • 结帐城市字段
  • 帐户编辑地址城市字段
  • 管理单订单页面:编辑计费和运输城市字段
  • 管理用户页面:编辑计费和航运城市字段

守则:

代码语言:javascript
复制
// Custom function that handle city options
function get_city_options() {
    $options = array(
        // '' => __( 'Select unit type' ),    /*= this will make empty field*/
        'Jeddah' => __('Jeddah', 'custom-domain'),
    );
    ksort($options);

    return $options;
}

// Checkout and my account (edit) billing and shipping city
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_city_fields' );
function custom_override_default_city_fields( $fields ) {
    $fields['city']['type'] = 'select';
    $fields['city']['class'] = array('my-field-class form-row-last');
    $fields['city']['input_class'] = array('state_select');
    $fields['city']['options'] = get_city_options();
    $fields['city']['priority'] = '41';

    return $fields;
}

// Admin editable single orders billing and shipping city field
add_filter('woocommerce_admin_billing_fields', 'admin_order_pages_city_fields');
add_filter('woocommerce_admin_shipping_fields', 'admin_order_pages_city_fields');
function admin_order_pages_city_fields( $fields ) {
    $fields['city']['type']    = 'select';
    $fields['city']['options'] = get_city_options();
    $fields['city']['class']   = 'short'; // Or 'js_field-country select short' to enable selectWoo (select2).

    return $fields;
}

// Admin editable User billing and shipping city
add_filter( 'woocommerce_customer_meta_fields', 'custom_override_user_city_fields' );
function custom_override_user_city_fields( $fields ) {
    $fields['billing']['fields']['billing_city']['type']    = $fields['shipping']['fields']['shipping_city']['type']  = 'select';
    $fields['billing']['fields']['billing_city']['options'] = $fields['shipping']['fields']['shipping_city']['options'] = get_city_options();

    return $fields;
}

代码在您的活动子主题(或活动主题)的functions.php文件中。测试和工作。

加载项-其他计费和发货字段

对于WooCommerce默认地址字段以外的其他计费和送货字段,您可以替换:

代码语言:javascript
复制
// Checkout and my account (edit) billing and shipping city
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_city_fields' );
function custom_override_default_city_fields( $fields ) {
    // … …
    return $fields;
}

由以下2项挂钩函数组成:

代码语言:javascript
复制
// For billing custom field (Checkout and my account edit addresses):
add_filter( 'woocommerce_billing_fields' , 'custom_override_billing_fields' );
function custom_override_billing_fields( $fields ) {
    $fields['billing_custom1'] = array(
        'type' => 'text', // chose the field type
        'label' =>  __('Custom label (billing)',  'woocommerce' ),
        'class' => array('form-row-wide'), // can be also 'form-row-first' or 'form-row-last'
        'required' => false, // Optional
        'clear' => true, // Optional
        'priority' => 200, // To change the field location increase or decrease this value
    );
    return $fields;
}

// For shipping custom field (Checkout and my account edit addresses):
add_filter( 'woocommerce_shipping_fields' , 'custom_override_shipping_fields' );
function custom_override_shipping_fields( $fields ) {
    $fields['shipping_custom1'] = array(
        'type' => 'text', // chose the field type
        'label' =>  __('Custom label (shipping)',  'woocommerce' ),
        'class' => array('form-row-wide'), // can be also 'form-row-first' or 'form-row-last'
        'required' => false, // Optional
        'clear' => true, // Optional
        'priority' => 200, // To change the field location increase or decrease this value
    );
    return $fields;
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64087397

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档