【问题标题】:WooCommerce dropdown list of cities in My Account pageWooCommerce 我的帐户页面中的城市下拉列表
【发布时间】:2020-08-12 16:14:33
【问题描述】:

我正在尝试添加一个下拉列表,并通过关注this code 在结帐页面上成功完成,

但我无法为“我的帐户”页面执行此操作。我知道我的帐户的代码不同,但无法弄清楚如何适应。

我在看this code。我添加了它的一些版本,现在它确实显示了一个下拉列表,唯一的选项是“数组”,而不是城市列表。

这是我的代码:

// Billing and Shipping fields on my account edit-addresses and checkout

add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
function custom_override_default_address_fields( $address_fields ) {

    $fields['city']['options'] = array(
    '' => __( 'Select your city' ),
    'Burnaby' => 'Burnaby',
    'Coquitlam' => 'Coquitlam',
    'Langley' => 'Langley',
    'New Westminster' => 'New Westminster',
    'North Vancouver' => 'North Vancouver',
    'Pitt Meadows' => 'Pitt Meadows',
    'Port Coquitlam' => 'Port Coquitlam',
    'Port Moody' => 'Port Moody',
    'Richmond' => 'Richmond',
    'Surrey' => 'Surrey',
    'West Vancouver' => 'West Vancouver'
);

    $address_fields['city']['type'] = 'select';
    $address_fields['city']['options'] = $fields;

    return $address_fields;
}

【问题讨论】:

    标签: php wordpress woocommerce hook-woocommerce


    【解决方案1】:

    你把$fields['city']['options']加到$address_fields['city']['options'] = $fields;有错误

    // Billing and Shipping fields on my account edit-addresses and checkout
    function custom_override_default_address_fields( $address_fields ) {
    
        $option_cities = array(
            '' => __( 'Select your city' ),
            'Burnaby' => 'Burnaby',
            'Coquitlam' => 'Coquitlam',
            'Langley' => 'Langley',
            'New Westminster' => 'New Westminster',
            'North Vancouver' => 'North Vancouver',
            'Pitt Meadows' => 'Pitt Meadows',
            'Port Coquitlam' => 'Port Coquitlam',
            'Port Moody' => 'Port Moody',
            'Richmond' => 'Richmond',
            'Surrey' => 'Surrey',
            'West Vancouver' => 'West Vancouver'
        );
    
        $address_fields['city']['type'] = 'select';
        $address_fields['city']['options'] = $option_cities;
    
        return $address_fields;
    }
    add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
    

    【讨论】:

    • 非常感谢!现在它看起来很明显并且有效。
    猜你喜欢
    • 2018-04-11
    • 1970-01-01
    • 2019-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-15
    • 1970-01-01
    • 2016-12-26
    相关资源
    最近更新 更多