• Hello guys,

    I ran into a problem with adding a custom field to the registration form on the woocommerce checkout form. On the registration form on my-account page I have everything in place and working, but I don’t get it to work on the checkout page. I succeeded with getting a custom field showing up by overriding the woocommerce tamplate (I know, bad practice), but I didn’t succeed with saving the value in the database. Again, on my-account page evrything is working with simillar code.

    What I tried is the following code:

    add_action(‘woocommerce_checkout_update_user_meta’, my_custom_checkout_field_update_user_meta’);
    function my_custom_checkout_field_update_user_meta( $user_id ) {
    if ($user_id && $_POST[‘group_id’]) {
    $wpdb->insert(
    ‘wp_groups_user_group’,
    array( ‘user_id’ => $user_id, ‘group_id’ => $_POST[‘group_id’] ),
    array( ‘%s’, ‘%s’ )
    );
    }
    }

    As you can see, it supposed to add the user to a group. However, the code is not working… It gives an internal error(from woocommerce) and the user gets registered assigned to the wrong group.

Viewing 1 replies (of 1 total)
  • Thread Starter sandeo

    (@sandeo)

    Okay, stupid me that was using the wrong hook and that forgot to call $wpdb. Problem solved, here is the code that worked:

    add_action(‘woocommerce_created_customer’, my_custom_checkout_field_update_user_meta’);
    function my_custom_checkout_field_update_user_meta( $user_id ) {
    if ($user_id && $_POST[‘group_id’]) {
    global $wpdb;
    $wpdb->insert(‘wp_groups_user_group’,array( ‘user_id’ => $user_id, ‘group_id’ => $_POST[‘group_id’] ),array( ‘%s’, ‘%s’ ));
    }
    }

Viewing 1 replies (of 1 total)
  • The topic ‘Adding a custom field to registration form on checkout’ is closed to new replies.