ARTICLE AD BOX
When a user registers in WooCommerce, I want to automatically create a new user in the Amelia plugin with the “Amelia Customer” role. Here is my code:
add_action('user_register', 'amelia_woocommerce_sync_on_registration', 10, 1); add_action('profile_update', 'amelia_woocommerce_sync_on_profile_update', 10, 2); /** * Synchronizing the user with Amelia during registration */ function amelia_woocommerce_sync_on_registration($user_id) { amelia_sync_user_to_amelia($user_id); } /** * Synchronizing the user with Amelia when updating the profile */ function amelia_woocommerce_sync_on_profile_update($user_id, $old_user_data) { // Let's check if the data has changed $current_user = get_userdata($user_id); if ($old_user_data->user_email !== $current_user->user_email || get_user_meta($user_id, 'first_name', true) !== get_user_meta($user_id, 'first_name', true) || get_user_meta($user_id, 'last_name', true) !== get_user_meta($user_id, 'last_name', true)) { amelia_sync_user_to_amelia($user_id); } } /** * The main function of user synchronization with Amelia */ function amelia_sync_user_to_amelia($user_id) { global $wpdb; // Skip the administrator $user = get_userdata($user_id); if (in_array('administrator', $user->roles)) { return; } // Retrieving user data $user_info = get_userdata($user_id); $user_email = $user_info->user_email; $first_name = get_user_meta($user_id, 'first_name', true); $last_name = get_user_meta($user_id, 'last_name', true); $phone_number = get_user_meta($user_id, 'billing_phone', true) ?: get_user_meta($user_id, 'phone_number', true); // Checking if a user already exists in Amelia $amelia_user_exists = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$wpdb->prefix}amelia_users WHERE externalId = %d", $user_id ) ); if ($amelia_user_exists) { // Updating an existing user $update_result = $wpdb->update( "{$wpdb->prefix}amelia_users", array( 'firstName' => $first_name, 'lastName' => $last_name, 'email' => $user_email, 'phone' => $phone_number ), array('externalId' => $user_id), array('%s', '%s', '%s', '%s'), array('%d') ); if (false === $update_result) { error_log('Amelia sync: Failed to update user ID ' . $user_id); } } else { // Add a new user with the role “amelia-customer” $insert_result = $wpdb->insert( "{$wpdb->prefix}amelia_users", array( 'firstName' => $first_name, 'lastName' => $last_name, 'email' => $user_email, 'phone' => $phone_number, 'type' => 'customer', 'externalId' => $user_id, 'note' => '', 'role' => 'amelia-customer' ), array('%s', '%s', '%s', '%s', '%s', '%d', '%s', '%s') ); // Assign a WordPress role to a user if ($insert_result !== false) { $user->add_role('amelia-customer'); } else { error_log('Amelia sync: Failed to insert user ID ' . $user_id); } } }Why do I need this? When a booking is made in the Amelia plugin, a WordPress user with the “Amelia Customer” role is automatically created. There are no issues with that.
But when placing an order or registering in WooCommerce, no user is created in the Amelia plugin. This is natural if there is no booking.
I want to set it up so that the user doesn’t have to register twice: once when placing an order and again when making a booking. Only one registration is needed for convenience.
Unfortunately, my code isn't working. I can't figure out what the problem is.
I think this code will be helpful to many people who use the Amelia plugin and WooCommerce on their websites. I hope you can help!
