3

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;
}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

1 Answers1

3

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;
}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Wow, thank you so much LoicTheAztec that worked a treat :):):) Using the old country dropdown field to trigger tax options based upon requirements: If From UK - VAT 20% for all If from outside the UK & buying for company - Sales Tax Zero If from outside the UK & buying as individual - Sales Tax 20% Works well now couldn't find a plugin that did that – Peter Rossetti Dec 12 '20 at 16:07
  • 1
    will do - new here so wasn't sure how – Peter Rossetti Dec 12 '20 at 16:23