• Resolved thebigtine

    (@josephbydesign)


    I have users registering through the plugin Ultimate Member. I am trying to register a billing address for woo commerce at the same time. For the address fields I just used the meta keys for the billing address, but I don’t want to do that for their first and last name and as their username is their phone number I can’t do it for that one ether. I wrote this simple plugin to try and rectify the problem, but when I go to checkout their name and phone number are blank. Their address is added successfully so I know the meta keys are right. I am using update_user_meta should I be using update_post_meta instead? Many thanks

    <?php
    /*
    Plugin Name: Autofill users info at checkout
    */
    
    function autofill_users_info_at_checkout_update_meta( $user_id ) {
    
        if ( isset( $_POST['user_login'] ) )
            update_user_meta($user_id, 'billing_phone', $_POST['user_login']);
    
        if ( isset( $_POST['first_name'] ) )
        	update_user_meta($user_id, 'billing_first_name', $_POST['first_name']);
    
        if ( isset( $_POST['last_name'] ) )
        	update_user_meta($user_id, 'billing_last_name', $_POST['last_name']);
    
    }
    add_action( 'user_register', 'autofill_users_info_at_checkout_update_meta', 10, 1 );
Viewing 13 replies - 1 through 13 (of 13 total)
  • I would say that the users add-on for Gravity Forms, is really easy to map form fields to user meta fields in a user feed.

    https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/

    Thread Starter thebigtine

    (@josephbydesign)

    Yeah, Gravity would of been my first choice but this is a clients project. I personally use gravity all the time and would of had this done ages ago with hidden fields using gravity.

    Moderator bcworkz

    (@bcworkz)

    Customer data is replicated in post_meta for each ‘shop_order’ post type, where the keys are prefixed with an underscore, as in ‘_billing_phone’. However, it appears the customer data on the checkout page comes from POST data through an AJAX request. I’m unable to absolutely verify where the AJAX data is coming from, but it does appear to be usermeta, not postmeta.

    I tried registering a new user on my site, then manually entered a billing_phone value in that user’s usermeta via phpMyAdmin. Upon going to the checkout page that phone number was shown correctly, so I’m not sure why your fields show up blank. Are you sure these fields you add on register actually exist in the DB? Are you sure the values are in $_POST when your callback fires?

    Thread Starter thebigtine

    (@josephbydesign)

    Well for starters, I don’t access to DB on this job ?? and what would be the easiest/quickest way to check the value of $_POST

    I am having trouble with the code again. I’ve recently lost all hosting access and had to start website from scratch, would anyone be able to help me with the code? i am using the same code

    .page-id-331 .standard_menu_type,
    .page-id-316 .standard_menu_type,
    .page-id-325 .standard_menu_type,
    .page-id-336 .standard_menu_type {
    display: none;
    }

    <style>
    /* CSS here */
    </style>

    but it is not removing headers.

    website is the same zullimethod.com

    Thread Starter thebigtine

    (@josephbydesign)

    Thanks luke, You have set me in the right direction. I used add_user_meta but with $_POST that didn’t add anything, but then I hard coded the value to add and it worked so it’s something with the $_POST now. any further ideas?

    @josephbydesign

    Share a gist of the code of what you currently have in the site plugin. Or share the code in a reply.

    Thread Starter thebigtine

    (@josephbydesign)

    @lukecavanagh

    here is the gist of what I have now: https://gist.github.com/josephbydeign/8c80f437152776ff4019491cdee92dbc

    I am just trying to get the first one to work then I will do likewise for the rest of them.

    As per the example on the user_register action hook codex page using $_POST should work.

    Thanks

    Moderator bcworkz

    (@bcworkz)

    IMO, despite Codex examples, values in $_POST cannot be relied on in this context. The function that fires ‘register_user’ action could be called in situations not involving form submits. I would have suggested pulling the values out of existing user meta in order to populate the Woo specific meta, but I see you have tried that.

    What about trying the ‘insert_user_meta’ filter? Your callback is passed the user’s nickname (defaults to username), first and last names, and other meta data in an array. You can use those passed values to populate the Woo specific meta.

    Thread Starter thebigtine

    (@josephbydesign)

    sounds promising! will try when I get home. thanks for reply!

    Thread Starter thebigtine

    (@josephbydesign)

    @bcworkz I can’t find any documentation about that filter. Can you give a link?

    Thread Starter thebigtine

    (@josephbydesign)

    @bcworkz Don’t worry I figured it out. As I was using Ultimate member I had to use some of their functions to make it work. Below is the final script for anyone else:

    function autofill_users_info_at_checkout_update_mata( $user_id ) {
    
    	um_fetch_user( $user_id );
            add_user_meta($user_id, 'billing_phone', um_user('user_login'));
            add_user_meta($user_id, 'billing_first_name', um_user('first_name'));
            add_user_meta($user_id, 'billing_last_name', um_user('last_name'));
    
    }
    add_action( 'user_register', 'autofill_users_info_at_checkout_update_mata', 100, 1 );

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Duplicate User meta field to another field on register’ is closed to new replies.