Registration country dropdown for woocommerce account
-
Hi,
On registration I provided a country dropdown (one of the default choices). I also use woocommerce. On registration I want to save the chosen country in the billing_country field of woocommerce. How can I achieve this?
Kind regards
Davy
-
Did you mean that you want to add the country field to the UM Register form and then once the form is submitted, it should update the
billing_countrymeta value of that newly registered user?Regards,
Hi,
Yes, that’s exactly what I mean.
Kind regards
DavyYou can try this code snippet to save the country to the billing_country meta.
add_action( 'um_registration_complete', 'um_021522_set_billing_country', 10, 2 ); function um_021522_set_billing_country( $user_id, $args ){ um_fetch_user( $user_id ); update_user_meta( $user_id, "billing_country", um_user("country") ); UM()->user()->remove_cache( $user_id ); }You can add the code to your theme/child-theme’s functions.php file or use the Code Snippet plugin to run the code.
Regards,
hi,
That doesn’t work. I already tried something equal. I think it’s due to the fact that woocommerce uses the 2 digit country code and the ultimate member field uses the country name in the local language…
Kind regards
DavyWe can fix it with the following code snippet:
add_filter( 'um_select_options_pair', 'um_021521_pair_country', 10, 2 ); function um_021521_pair_country( $is_pairing, $data ){ if( "country" == $data['metakey'] ){ return true; } return $is_pairing; }This will make the country code the country field’s selected value.
Regards,
Hi,
Unfortunately this doesn’t work. When I inspect the dropdown I see values 0,1,2,… So each dropdown value has a number, not a country code.
Kind regards
Davy@eyewebdesign
@champsupertrampYes, Woocommerce is using the ISO 3166 two character country codes.
I have made an UM New Feature request here:
https://github.com/ultimatemember/ultimatemember/issues/980
-
This reply was modified 4 years, 1 month ago by
missveronica.
-
This reply was modified 4 years, 1 month ago by
missveronica.
You can try this updated code snippet using the UM builtin country codes:
add_action( 'um_registration_complete', 'um_021522_set_billing_country', 10, 2 ); function um_021522_set_billing_country( $user_id, $args ){ um_fetch_user( $user_id ); $country_codes = UM()->builtin()->get( 'countries' ); if( in_array( um_user( "country" ), $country_codes )) { update_user_meta( $user_id, "billing_country", array_search( um_user( "country" ), $country_codes )); } UM()->user()->remove_cache( $user_id ); }Hi,
Thanks, but still no success. I use the code below:
add_action( 'um_registration_complete', 'eye_set_billing_country', 10, 2 ); function eye_set_billing_country( $user_id, $args ){ um_fetch_user( $user_id ); update_user_meta( $user_id, "billing_country", um_user("country") ); $country_codes = UM()->builtin()->get( 'countries' ); if( in_array( um_user( "country" ), $country_codes )) { update_user_meta( $user_id, "billing_country", array_search( um_user( "country" ), $country_codes )); } UM()->user()->remove_cache( $user_id ); } add_filter( 'um_select_options_pair', 'eye_pair_country', 10, 2 ); function eye_pair_country( $is_pairing, $data ){ if( "country" == $data['metakey'] ){ return true; } return $is_pairing; }Don’t use the second code snippet with pairing only the code snippet in my post.
I have tested my code snippet and it works together with WooCommerce.That works indeed. Thanks a lot for helping!
Thanks for letting us know.
Regards,
-
This reply was modified 4 years, 1 month ago by
The topic ‘Registration country dropdown for woocommerce account’ is closed to new replies.