Action uncanny-learndash-groups

uo_new_group_purchased

Fires after a user successfully purchases a new group, providing the group and user IDs.

add_action( 'uo_new_group_purchased', $callback, 10, 2 );

Description

Fires after a new group is successfully purchased by a user. Developers can leverage this hook to perform custom actions, such as sending notifications, updating user roles, or integrating with other systems, immediately following a group purchase. `$group_id` represents the purchased group's ID, and `$user` is the user object who made the purchase.


Usage

add_action( 'uo_new_group_purchased', 'your_function_name', 10, 2 );

Parameters

$group_id (mixed)
The `$group_id` parameter contains the unique identifier of the group that was just purchased.
$user (mixed)
This parameter contains the ID of the newly purchased group.

Examples

/**
 * This function is triggered when a new group is purchased.
 * It demonstrates how to use the 'uo_new_group_purchased' action hook.
 * In this example, we'll update a custom user meta field to track the number of groups purchased by a user.
 *
 * @param int $group_id The ID of the newly purchased group.
 * @param int $user_id The ID of the user who purchased the group.
 */
add_action( 'uo_new_group_purchased', 'my_plugin_track_group_purchases', 10, 2 );

function my_plugin_track_group_purchases( $group_id, $user_id ) {
    // Ensure we have valid user ID and group ID.
    if ( ! $user_id || ! $group_id ) {
        return;
    }

    // Get the current count of groups purchased by the user.
    $group_purchase_count = get_user_meta( $user_id, 'total_groups_purchased', true );

    // If it's the first purchase, initialize the count to 1.
    if ( empty( $group_purchase_count ) ) {
        $group_purchase_count = 0;
    }

    // Increment the count.
    $group_purchase_count++;

    // Update the user meta with the new count.
    update_user_meta( $user_id, 'total_groups_purchased', $group_purchase_count );

    // Optionally, you could log this event or send a notification.
    // For demonstration, we'll just add a simple log entry.
    error_log( "User ID: {$user_id} has purchased group ID: {$group_id}. Total groups purchased: {$group_purchase_count}" );
}

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/woocommerce/woocommerce-license-subscription.php:640
src/classes/woocommerce/woocommerce-license.php:1100

//Setting group leader as a member of the group & using 1 code / qty for it.
		if ( 'yes' !== get_option( 'do_not_add_group_leader_as_member', 'no' ) ) {

			Group_Management_Helpers::add_existing_user( array( 'user_email' => $user->user_email ), true, $group_id, $order_id, SharedFunctions::$redeem_status, false );
		}
		SharedFunctions::remove_transient_cache( 'all' );
		do_action( 'uo_new_group_purchased', $group_id, $user->ID );
	}

	/**
	 * @param WC_Subscription $subscription
	 */
	public function change_group_to_draft( WC_Subscription $subscription ) {
		if ( true !== apply_filters( 'ulgm_subscription_status_switch_group_to_draft', true, current_action(), $subscription, $this ) ) {


Scroll to Top