3

in the checkout of my Woocommerce-Shop is a dropdown to choose your country. On default the USA is automatically selected already. How to just have a placeholder with “select your country” instead?

I couldn’t find any solution to this topic somebody have any idea?

I changed the other Placeholders which aren't Dropdowns but are inside the same form:

add_filter( 'woocommerce_checkout_fields' , 'override_billing_checkout_fields', 20, 1 );
function override_billing_checkout_fields( $fields ) {
    $fields['billing']['billing_first_name']['placeholder'] = 'First Name*';
    $fields['billing']['billing_last_name']['placeholder'] = 'Last Name*';
    $fields['billing']['billing_city']['placeholder'] = 'Town / City*';
    $fields['billing']['billing_postcode']['placeholder'] = 'ZIP*';
    $fields['billing']['billing_email']['placeholder'] = 'Email Address*';
    return $fields;
}

My poor solution for now is: I created a new "Country in the Dropdown List" which I just named: select your country* and then just selected it on default instead of the USA. But the problem is here that the system thinks a real country is chosen already so it's not a mandatory field anymore and also the fact that it just doesn't look usual for the user when they choose their country:

function woo_add_my_country( $country ) {
   $country["PLACE"] = 'select your country*';
   return $country;
}
add_filter( 'woocommerce_countries', 'woo_add_my_country', 10, 1 );


add_filter( 'default_checkout_billing_country', 'bbloomer_change_default_checkout_country' );

function bbloomer_change_default_checkout_country() {
  return 'PLACE'; 
}

I would appreciate any help or tips!

mauricem
  • 59
  • 6

3 Answers3

4

This is working for my side try this

// Change the default country and state on checkout page. 
// This works for a new session.
add_filter( 'default_checkout_country', 'xa_set_default_checkout_country' );
add_filter( 'default_checkout_state', 'xa_set_default_checkout_state' );
function xa_set_default_checkout_country() {
  // Returns empty country by default.
    return null;
  // Returns India as default country.
     // return 'IN';
}

function xa_set_default_checkout_state() {
  // Returns empty state by default.
    return null;
  // Returns Madhya Pradesh as default state.
     // return 'MP';
}
function woo_add_my_country( $country ) {
   $country["PLACE"] = 'select your country*';
   return $country;
}
add_filter( 'woocommerce_countries', 'woo_add_my_country', 10, 1 );


add_filter( 'default_checkout_billing_country', 'bbloomer_change_default_checkout_country' );

function bbloomer_change_default_checkout_country() {
  return 'PLACE'; 
}
rajat.gite
  • 450
  • 2
  • 10
  • Hey, thank you this already fixed one of the problem that it is now a mandatory field. But the "select your country*" is still not a placeholder which just disappear after clicking the dropdown. – mauricem Feb 13 '20 at 12:44
1

Just go to WooCommerce > Settings. On General tab you will find Default customer location. Set it to No location by default

enter image description here

Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139
  • Thank you for the tipp, but this doesn't add a placeholder text inside the dropdown in my case. – mauricem Feb 13 '20 at 12:38
  • @mauricem yes it won't.. you should use same as how you did it with other fields using `woocommerce_checkout_fields` filter – Reigel Gallarde Feb 13 '20 at 12:41
  • The problem with this solution is that also other functions are based on the geolocation of the users. Tax, currency, etc. You also know how to set the placeholder without changing the default customer location? – mauricem Feb 13 '20 at 13:32
1

This was one of the first results that popped up for me when searching how to set a placeholder for a select field on the Woocommerce checkout page, so for any future visitors:

I have a company select field that I populate dynamically with values from the database. In order to set a placeholder value, I just add an option with a blank value.

add_filter( 'woocommerce_checkout_fields' , 'populate_company_field' );
function populate_company_field($fields) {
  $results = get_results_from_database();
  $options[''] = __('Choose your company', 'gs'); //add blank value = placeholder

  foreach ($results as $result) {
    if (!empty($result->company)) {
        $options[$result->ID] = $result->company;
    }
  }

  $fields['billing']['company']['options'] = $options;

  return $fields;
}
Maxime
  • 29
  • 4