Action uncanny-learndash-groups

ulgm_existing_group_user_added

Fires when a user is successfully added to an existing group, providing user, group, and order data.

add_action( 'ulgm_existing_group_user_added', $callback, 10, 3 );

Description

Fires after a user is successfully added to an existing group, often during checkout. Developers can use this to trigger custom actions like sending notifications, updating user roles, or integrating with other services based on the user data, group ID, and associated order ID.


Usage

add_action( 'ulgm_existing_group_user_added', 'your_function_name', 10, 3 );

Parameters

$user_data (mixed)
This parameter contains the data of the user who has been added to an existing group.
$group_id (mixed)
This parameter contains the data for the user being added to an existing group.
$order_id (mixed)
This parameter contains the unique identifier for the group the user was added to.

Examples

// Hook into the ulgm_existing_group_user_added action to send a notification to the user.
add_action( 'ulgm_existing_group_user_added', 'my_custom_notification_for_existing_group_user', 10, 3 );

/**
 * Sends a custom notification to a user when they are added to an existing group.
 *
 * This function serves as an example of how to hook into the 'ulgm_existing_group_user_added'
 * action to perform custom logic, such as sending an email notification.
 *
 * @param mixed $user_data  The data of the user being added. This could be a user ID,
 *                          a WP_User object, or an array of user details.
 * @param mixed $group_id   The ID of the group the user is being added to.
 * @param mixed $order_id   The ID of the WooCommerce order associated with this addition, if any.
 */
function my_custom_notification_for_existing_group_user( $user_data, $group_id, $order_id ) {

	// Ensure we have a valid user ID to proceed.
	$user_id = 0;
	if ( is_numeric( $user_data ) ) {
		$user_id = absint( $user_data );
	} elseif ( is_array( $user_data ) && isset( $user_data['ID'] ) ) {
		$user_id = absint( $user_data['ID'] );
	} elseif ( is_object( $user_data ) && property_exists( $user_data, 'ID' ) ) {
		$user_id = absint( $user_data->ID );
	}

	if ( ! $user_id ) {
		error_log( 'ulgm_existing_group_user_added: Could not determine user ID for notification.' );
		return;
	}

	// Get the WP_User object.
	$user = get_user_by( 'id', $user_id );

	if ( ! $user ) {
		error_log( 'ulgm_existing_group_user_added: User not found for ID: ' . $user_id );
		return;
	}

	// Get group details for the email.
	$group = get_term_by( 'id', $group_id, 'group' ); // Assuming 'group' is the taxonomy name.

	if ( ! $group ) {
		error_log( 'ulgm_existing_group_user_added: Group not found for ID: ' . $group_id );
		// Still proceed with notification, as group might be deleted or invalid.
		$group_name = __( 'a group', 'my-textdomain' );
	} else {
		$group_name = $group->name;
	}

	// Construct the email subject.
	$email_subject = sprintf(
		__( 'You have been added to the group "%s"', 'my-textdomain' ),
		$group_name
	);

	// Construct the email message.
	$email_message = sprintf(
		__( 'Hello %s,', 'my-textdomain' ) . "nn" .
		__( 'You have been automatically added to the group "%s".', 'my-textdomain' ) . "nn" .
		__( 'You can now access the resources and discussions within this group.', 'my-textdomain' ) . "nn" .
		__( 'Thank you,', 'my-textdomain' ) . "n" .
		__( 'Your Website Team', 'my-textdomain' ),
		$user->display_name,
		$group_name
	);

	// Add WooCommerce order context if available.
	if ( $order_id && absint( $order_id ) > 0 ) {
		$order = wc_get_order( $order_id );
		if ( $order ) {
			$order_link = '<a href="' . esc_url( $order->get_view_order_url() ) . '">' . __( 'order', 'my-textdomain' ) . '</a>';
			$email_message .= "nn" . sprintf(
				__( 'This addition was associated with your recent %s (Order #%s).', 'my-textdomain' ),
				$order_link,
				$order->get_order_number()
			);
		}
	}

	// Send the email. You would typically use wp_mail() for this.
	$sent = wp_mail( $user->user_email, $email_subject, $email_message );

	if ( ! $sent ) {
		error_log( 'ulgm_existing_group_user_added: Failed to send notification email to ' . $user->user_email . ' for group ID ' . $group_id );
	}
}

// Example of a filter hook - let's say we want to modify the user data before it's processed.
// This is a hypothetical example as the provided context doesn't show a filter for $user_data,
// but demonstrates how a filter might be used.
add_filter( 'ulgm_user_data_before_group_addition', 'my_custom_user_data_modification', 10, 1 );

/**
 * Modifies user data before it's added to a group.
 *
 * This is a hypothetical filter hook example. It demonstrates how you might alter
 * user data before it's passed to the core group addition logic.
 *
 * @param mixed $user_data The user data to be modified.
 * @return mixed The modified user data.
 */
function my_custom_user_data_modification( $user_data ) {
	// Example: If user_data is an array and contains a 'role' key, perhaps we want to ensure
	// a specific role is assigned or checked.
	if ( is_array( $user_data ) && isset( $user_data['role'] ) ) {
		// For demonstration, let's ensure the role is lowercase.
		$user_data['role'] = strtolower( $user_data['role'] );
	}

	// Return the potentially modified user data.
	return $user_data;
}

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/classes/group-management/class-group-management-helpers.php:767
src/classes/woocommerce/woocommerce-modify-group.php:552

if ( $is_api ) {
					wp_send_json_error( $data );
				}
			}
		}

		do_action( 'ulgm_existing_group_user_added', $user_data, $group_id, $order_id );
		$do_not_send_emails = apply_filters( 'ulgm_do_not_send_existing_user_email', $do_not_send_emails, $user_data, $group_id, $order_id );

		if ( $do_not_send_emails ) {

			$data['message'] = __( 'The specified user was already registered on the site and has been automatically added to this group.', 'uncanny-learndash-groups' );

			if ( $counter ) {


Scroll to Top