1

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.

starball
  • 20,030
  • 7
  • 43
  • 238
  • Try changing the hook priority, – Akshay Paghdar Jan 10 '23 at 22:03
  • In user_register you can check if user is in a team if not to assign him or find the hook that assign him and trigger wp_new_user_notification there. Its premium plugin and cant help much about it :) – Snuffy Jan 11 '23 at 08:31
  • @AkshayPaghdar, thanks for your reply. I tried that but it did not work. I used 9999. – George Fragkos Jan 11 '23 at 13:52
  • @MartinMirchev thanks for your reply as well. The accounts are being created after the user is invited by the team's owner. So, they are being assigned to the corresponding teams automatically after they create their accounts. I need to find something that will fire the email after the assignment to the team takes place. Unfortunately, the plugin's support told that there is not such a hook from their side. – George Fragkos Jan 11 '23 at 13:58
  • Then create your own check. What i think will work is using profile_update hook - https://developer.wordpress.org/reference/hooks/profile_update/ . As you can see fires when profile is updated so in your function check if user has assigned team or its changed and trigger the email notification. – Snuffy Jan 11 '23 at 14:53
  • @MartinMirchev, this sounds like a good workaround! I am going to test that and get back to you. Thanks so much again! – George Fragkos Jan 11 '23 at 17:56
  • @MartinMirchev, I have just tried that but with no luck. My users are being assigned to teams upon their registration. But, I guess the action user_register I mentioned initially on my code is firing just before the teams assignments and that is why I see no team value on my admin email. The profile_update did not do anything since there is no actual updating of the profile upon registration. – George Fragkos Jan 12 '23 at 08:35
  • Can you confirm that you get anything in wc_memberships_for_teams_get_teams() ? Can you debug and see what you get ? – Snuffy Jan 12 '23 at 08:45
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/251066/discussion-between-martin-mirchev-and-george-fragkos). – Snuffy Jan 12 '23 at 08:49

1 Answers1

0

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!