• Resolved eyewebdesign

    (@eyewebdesign)


    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

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @eyewebdesign

    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_country meta value of that newly registered user?

    Regards,

    Thread Starter eyewebdesign

    (@eyewebdesign)

    Hi,

    Yes, that’s exactly what I mean.

    Kind regards
    Davy

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @eyewebdesign

    You 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,

    Thread Starter eyewebdesign

    (@eyewebdesign)

    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
    Davy

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @eyewebdesign

    We 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,

    Thread Starter eyewebdesign

    (@eyewebdesign)

    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
    @champsupertramp

    Yes, 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 3 years, 1 month ago by missveronica.
    • This reply was modified 3 years, 1 month ago by missveronica.

    @eyewebdesign

    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 );
    }
    Thread Starter eyewebdesign

    (@eyewebdesign)

    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;
    }

    @eyewebdesign

    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.

    Thread Starter eyewebdesign

    (@eyewebdesign)

    That works indeed. Thanks a lot for helping!

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Thanks for letting us know.

    Regards,

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Registration country dropdown for woocommerce account’ is closed to new replies.