Change WooCommerce checkout country field default option displayed label

醉酒当歌 提交于 2021-01-28 02:58:16

问题


How would edit the default value on and existing array - have managed to edit the label using this but cannot get the options property to work using this:

add_filter( 'woocommerce_checkout_fields', 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
    $fields['billing']['billing_country']['options value="default"'] = 'My new select prompt';
    $fields['billing']['billing_country']['label'] = 'Are you purchasing as a Company Y/N or from the UK?';
    
    return $fields;
}

回答1:


To change WooCommerce country field default option displayed value in checkout page, you can use the following composite hook this way:

add_filter( 'woocommerce_form_field_country', 'filter_form_field_country', 10, 4 );
function filter_form_field_country( $field, $key, $args, $value ) {
    // Only in checkout page for "billing" city field
    if ( is_checkout() && 'billing_country' === $key ) {
        $search  = esc_html__( 'Select a country / region…', 'woocommerce' ); // String to search
        $replace = esc_html__( 'My new select prompt', 'woocommerce' ); // Replacement string
        $field   = str_replace( $search, $replace, $field );
    }
    return $field;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Then you can keep on your question code the following:

add_filter( 'woocommerce_checkout_fields', 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
    $fields['billing']['billing_country']['label'] = __('Are you purchasing as a Company Y/N or from the UK?', 'woocommerce');
    
    return $fields;
}


来源:https://stackoverflow.com/questions/65265005/change-woocommerce-checkout-country-field-default-option-displayed-label

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!