Okay
  Public Ticket #3438805
Automatically create Amelia customer when WP user with role 'Amelia Customer' is created
Closed

Comments

  • Christian Graf started the conversation

    Hello Support,

    I do have a big problem with creating amelia customers. People have to create an account to book courses on the website and so they are only visible for logged in users. 

    But: I do not find a way to simultaneously create an Amelia customer when a WordPress user is created. 

    I already found this ticket https://tmsplugins.ticksy.com//ticket/2739915/ but the code is not working for me, it does nothing and not even throws an error.

    Could anybody help me please? This is really critical for the whole business process in this project.

    Thanks,
    Christian

  • Christian Graf replied

    I found the solution! I had to adopt the table-prefix. wp_amelia_users uses the generic table-prefix, changing this to xyzwhatever_amelia_users did the job. This is twice in the code. I also tweaked the username lines a bit. user_firstname and user_lastname are a bit old – property names used in WordPress prior to version 2.0.0 (14 years ago ...) –  (user_first_name and user_last_name from the original code-suggestion was wrong at all) and I replaced it with first_name and last_name. So here is the whole working code for the functions.php:

    /* ==========================================================================
     * #create the user when someone registers on your website, without booking an appointment
    ============================================================================*/
    add_action( 'user_register', 'custom_amelia_registration_save', 10, 1 );
    function custom_amelia_registration_save($user_id) {
        global $wpdb;
        $wpUser = get_user_by('ID', $user_id);
        $ameliaUserId = $wpdb->get_col(
            "SELECT id FROM xyzprefix_amelia_users WHERE email = '{$wpUser->user_email}'"
        );
        if (!$ameliaUserId && in_array('wpamelia-customer', $wpUser->roles)) {
            $ameliaUserFirstName = $wpUser->first_name ?: $wpUser->user_nicename;
            $ameliaUserLastName = $wpUser->last_name ?: $wpUser->user_nicename;
            $wpdb->query("
                INSERT INTO xyzprefix_amelia_users
                (
                type,
                status,
                externalId,
                firstName,
                lastName,
                email
                ) VALUES (
                'customer',
                'visible',
                {$user_id},
                '{$ameliaUserFirstName}',
                '{$ameliaUserLastName}',
                '{$wpUser->user_email}'
                )
            ");
        }

    If anyone could add an extention to check if the user is email-approved (by eg. help of Ultimate Member Plugin) – that would be a great extension!

    Thanks
    Christian

  •  1,172
    Uroš replied

    Hello Christian,

    Thank you for sharing this with us and other users, someone will find it useful for sure.

    If you have any other questions, please let me know.


    Kind Regards, 

    Uros Jovanovic
    [email protected]

    Rate my support

    wpDataTables: FAQ | Facebook | Twitter | InstagramFront-end and back-end demo | Docs

    Amelia: FAQ | Facebook | Twitter | InstagramAmelia demo sites | Docs | Discord Community

    You can try wpDataTables add-ons before purchasing on these sandbox sites:

    Powerful Filters | Gravity Forms Integration for wpDataTables | Formidable Forms Integration for wpDataTables | Master-Detail Tables