Filter uncanny-learndash-groups

ulgm_redeem_code_for_current_logged_in_user

Filters the result of redeeming a code for the current logged-in user.

add_filter( 'ulgm_redeem_code_for_current_logged_in_user', $callback, 10, 5 );

Description

This filter hook allows developers to modify the redemption process for a logged-in user's code. It fires before a code is marked as used. Developers can return `false` to prevent code redemption entirely or manipulate the provided parameters to alter the outcome. The default behavior is to allow redemption if the code is valid.


Usage

add_filter( 'ulgm_redeem_code_for_current_logged_in_user', 'your_function_name', 10, 5 );

Parameters

$entry (mixed)
This parameter is a boolean flag that determines whether the code redemption process should proceed or be aborted.
$form (mixed)
This parameter contains the Gravity Forms entry object associated with the redemption.
$user_id (mixed)
This parameter contains the WordPress `$form` object that the code redemption is associated with.
$code_redemption (mixed)
The `$code_redemption` parameter is used to pass the actual code that the user is attempting to redeem.
$code_details (mixed)
This parameter contains the details of the redeemed code, which is likely an array or object holding information about the code's properties and status.

Return Value

The filtered value.


Examples

/**
 * Example of how to use the ulgm_redeem_code_for_current_logged_in_user filter.
 *
 * This function demonstrates how to intercept the code redemption process for a logged-in user.
 * In this example, we'll prevent redemption if the user already has a code associated with them,
 * and we'll also add a custom user meta field upon successful redemption.
 *
 * @param mixed $original_return_value The original return value from the filter (default is true).
 * @param array $entry                 The Gravity Forms entry array.
 * @param object $form                The Gravity Forms form object.
 * @param int $user_id                The ID of the currently logged-in user.
 * @param string $code_redemption     The redemption code being used.
 * @param array $code_details         An array containing details about the redemption code.
 *
 * @return mixed The modified return value. Returning false will prevent the redemption.
 */
add_filter( 'ulgm_redeem_code_for_current_logged_in_user', function ( $original_return_value, $entry, $form, $user_id, $code_redemption, $code_details ) {

	// Check if the user already has a code redeemed.
	$existing_code = get_user_meta( $user_id, '_ulgm_code_used', true );
	if ( ! empty( $existing_code ) ) {
		// Optionally, you could add an error notice here for the user.
		// GFCommon::add_error_message( __( 'You have already redeemed a code.', 'your-text-domain' ) );
		return false; // Prevent redemption if the user already has a code.
	}

	// If the code redemption is successful, add a custom user meta.
	if ( 'success' === $code_details['result'] ) {
		update_user_meta( $user_id, '_ulgm_last_redeemed_code', $code_redemption );
		// You can perform other actions here, like granting access to a course via LearnDash.
		// The original function's logic for adding to a group is handled elsewhere,
		// but you could integrate it here if needed or if this filter is intended
		// to replace or augment that.
	}

	// Return the original or modified value. Returning false will stop the redemption.
	return $original_return_value;
}, 10, 6 );

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/gravity-forms/gravity-forms-code-field.php:246

public function redeem_key( $code_redemption, $user_id, $entry, $form ) {
		$code_details = SharedFunctions::is_key_available( $code_redemption );

		if ( true !== apply_filters( 'ulgm_redeem_code_for_current_logged_in_user', true, $entry, $form, $user_id, $code_redemption, $code_details ) ) {
			return;
		}

		if ( 'success' === $code_details['result'] ) {
			update_user_meta( $user_id, '_ulgm_code_used', $code_redemption );
			$ld_group_id = (int) $code_details['ld_group_id'] ?? 0;
			$result      = ulgm()->group_management->set_user_to_code( $user_id, $code_redemption, SharedFunctions::$not_started_status, $ld_group_id );
			if ( $result ) {
				SharedFunctions::set_user_to_group( $user_id, $code_details['ld_group_id'] );
			}
		}
	}

Scroll to Top