I was wondering to know if it is possible to run a WordPress function with a delay of some seconds or minutes.
I use the following snippet to send an email to admin after a new user is registered on my website
add_action( 'user_register', 'registration_email_to_admin', 10, 1 );
function registration_email_to_admin( $user_id ) {
wp_new_user_notification( $user_id );
}
Then I use the following code to filter the email's content
//Filters the contents of the new user notification email sent to the site admin.
add_filter( 'wp_new_user_notification_email_admin', 'custom_wp_new_user_notification_admin_email', 10, 3 );
function custom_wp_new_user_notification_admin_email( $wp_new_user_notification_email, $user, $blogname ) {
//pass user id into a variable
$user_id = $user->id;
//get teams for that user
$teams = wc_memberships_for_teams_get_teams( $user_id );
$teamNames = [];
foreach($teams as $teamName){
array_push($teamNames, $teamName->get_name());
}
$teamNamesString = implode (',' , $teamNames);
$wp_new_user_notification_email['subject'] = sprintf( '[%s] New user registered. User ID = %s', $blogname, $user->id );
$wp_new_user_notification_email['message'] = sprintf( "%s ( %s ) has registered to the website %s.", $user->user_login, $user->user_email, $blogname ) . "\r\n\r\n";
$wp_new_user_notification_email['message'] .= sprintf(__('Username: %s'), $user->user_login) . "\r\n";
$wp_new_user_notification_email['message'] .= sprintf(__('Email: %s'), $user->user_email) . "\r\n";
$wp_new_user_notification_email['message'] .= sprintf(__('ID: %s'), $user->id) . "\r\n";
$wp_new_user_notification_email['message'] .= sprintf(__('Team Name: %s'), $teamNamesString) . "\r\n";
$wp_new_user_notification_email['headers'] = "From: Members Website <info@test.com> \n\r cc: GeorgeFR <test@yahoo.com>";
return $wp_new_user_notification_email;
}
On all the above, everything works well except the last append to the message
$wp_new_user_notification_email['message'] .= sprintf(__('Team Name: %s'), $teamNamesString) . "\r\n";
Using the WooCommerce Memberships for Teams plugin, it seems that the new user is being assigned to the team that has been invited to after his/her registration. That being said, the variable $teamNamesString
is always empty on my email content.
So, is it possible to run all the above with some delay, making sure that the team has been assigned to the new user before I send the email? It is important for me to include the team's name to the email for business management purposes.
I was finally able to find the fix here.
Using the Woo Memberships and Woo Teams for Memberships plugins, we have 3 different team roles on our website. Owners, Managers, and Members. Also, we are using Woo Subscriptions in combination with Woo Memberships. So, every time a new subscription purchase comes, the purchaser will create a team and become the owner of that team.
The codes that worked are the following:
For Owners (used the woocommerce_subscription_status_active
action from Woo Subscriptions):
//fires after purchases
function send_admin_email_with_owner_data_after_purchase( $subscription ) {
// Get the user in the order
$user_id = $subscription->get_user_id();
// Get the products in the order
$items = $subscription->get_items();
foreach( $items as $item ) {
$product = $item->get_product();
$product_id = $product->get_id();
if ($product_id == "8865") {
wp_new_user_notification( $user_id );
}
}
}
add_action( 'woocommerce_subscription_status_active', 'send_admin_email_with_owner_data_after_purchase', 10 );
For Managers and Members (used the wc_memberships_user_membership_created
action from Woo Memberships):
//fires after acceptance of invitations to teams (in my case)
function send_admin_email_with_member_data( $plan, $args ) {
$user_id = $args['user_id'];
$memberArgs = array (
$status => "any",
$role => "manager,member"
);
//get teams for that user
$teams = wc_memberships_for_teams_get_teams( $user_id, $memberArgs );
if ( $args['is_update'] = true && ! empty( $teams ) ) {
wp_new_user_notification( $user_id );
}
}
add_action( 'wc_memberships_user_membership_created', 'send_admin_email_with_member_data', 20, 2 );
I am glad I found a way here. Thank you all for your help!