What I'm NOT trying to do:
- Remove or hide checkout (billing/shipping) country fields
What I'm trying do to:
- Show only ONE country in checkout (billing/shipping) country fields, depending on geolocation (geolocation is already done)
- OR, Remove countries I don't want to show to user from checkout (billing/shipping) country fields
Facts:
- My Woocommerce Selling Location(s) settings are: Germany, Andorra, Austria, Bulgaria, Belgium, Croatia, Denmark, Slovakia, Slovenia, Spain, Estonia, Finland, France, Greece, Hungary, Ireland, Latvia, Lithuania, Luxembourg, Monaco, Netherlands, Poland, Portugal, United Kingdom (UK), Czech Republic, Romania, Sweden and Switzerland
- I want to sell to all of these countries, so removing countries from Selling Location(s) list is not a solution.
- I need a hook/filter solution, please.
What have I've done so far:
- I've tried out several hooks/filters to achieve that:
woocommerce_checkout_get_value
,woocommerce_shipping_fields
,woocommerce_billing_fields
,woocommerce_default_address_fields
andwoocommerce_checkout_fields
. None of them seems to work...
Code I've written:
// NOT WORKING! SHOWS ALL Selling Location(s) (allowed countries)
function override_checkout_fields( $fields ) {
$fields['shipping']['shipping_country'] = array(
'type' => 'country',
'label' => __( 'Country / Region', 'woocommerce' ),
'required' => true,
'class' => array( 'form-row-wide', 'address-field', 'update_totals_on_change' ),
'autocomplete' => 'country',
'priority' => 40,
'options' => array( $user_country_code => __( $user_country_name, 'woocommerce' ) )
);
//Same for billing_country...
return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'override_checkout_fields', 10, 1 );
// NOT WORKING! SHOWS ALL Selling Location(s) (allowed countries)
function edit_billing_fields( $fields ) {
$fields['billing']['billing_country'] = array(
'type' => 'country',
'label' => __( 'Country / Region', 'woocommerce' ),
'required' => true,
'class' => array( 'form-row-wide', 'address-field', 'update_totals_on_change' ),
'autocomplete' => 'country',
'priority' => 40,
'options' => array( $user_country_code => __( $user_country_name, 'woocommerce' ) )
);
return $fields;
}
add_filter( 'woocommerce_billing_fields', 'edit_billing_fields' );
... And more
NOTE: If I change 'type' => 'country'
by 'type' => 'select'
it works, but then if I change country (showing two countries for testing), the state dropdown do not update whith the states for that new selected country. Also the dropdown is shown without Select2 appeareance.
Further Explanation: I am selling now to 28 countries. For example, if a user is accessing the web from France, when the user arrives at checkout form, I only want to let you send the product to France. This is the reason why I want to show only the user country (previously geolocated) in the (billing/shipping) country dropdown.
I would appreciate your help! Thanks