ulc_redeem_code_for_current_logged_in_user
Filters the redemption of a coupon code for the currently logged-in user after it has been applied.
add_filter( 'ulc_redeem_code_for_current_logged_in_user', $callback, 10, 5 );
Description
Filters the outcome of redeeming a membership code for the current logged-in user via Gravity Forms. Developers can use this hook to alter the redemption process or perform custom actions based on the entry, form, user ID, code redemption status, and coupon ID before the process is finalized.
Usage
add_filter( 'ulc_redeem_code_for_current_logged_in_user', 'your_function_name', 10, 5 );
Parameters
-
$entry(mixed) - This parameter is a boolean value indicating whether the code redemption process should continue.
-
$form(mixed) - This parameter contains information about the submitted Gravity Forms entry.
-
$user_id(mixed) - This parameter contains the Gravity Forms entry object associated with the current form submission.
-
$code_redemption(mixed) - This parameter contains the ID of the currently logged-in user for whom the code redemption is being processed.
-
$coupon_id(mixed) - This parameter contains the ID of the coupon that was redeemed.
Return Value
The filtered value.
Examples
add_filter( 'ulc_redeem_code_for_current_logged_in_user', 'my_custom_redeem_code_logic', 10, 6 );
/**
* Example of how to modify the default code redemption logic for logged-in users.
* This custom function could, for instance, prevent redemption for certain coupon types
* or add additional checks before allowing the code to be redeemed.
*
* @param mixed $return_value The default return value (true or false) indicating if the code should be redeemed.
* @param mixed $entry The Gravity Forms entry object.
* @param mixed $form The Gravity Forms form object.
* @param int $user_id The ID of the currently logged-in user.
* @param string $code_redemption The actual enrollment code entered by the user.
* @param int|false $coupon_id The ID of the available coupon if found, otherwise false.
*
* @return mixed The modified return value. Return false to prevent redemption.
*/
function my_custom_redeem_code_logic( $return_value, $entry, $form, $user_id, $code_redemption, $coupon_id ) {
// Example: Prevent redemption if the coupon is of a specific type (e.g., for testing)
if ( $coupon_id && 'test_coupon_123' === Database::get_coupon_slug( $coupon_id ) ) {
// Add a notification to the user or log this event
error_log( "Attempted to redeem a restricted test coupon ({$code_redemption}) by user ID {$user_id}." );
return false; // Prevent redemption for this specific coupon
}
// Example: Add an additional check to ensure the user hasn't redeemed a similar course recently
$last_redeemed_course_time = get_user_meta( $user_id, 'last_course_redemption_time', true );
if ( $last_redeemed_course_time && ( time() - $last_redeemed_course_time < DAY_IN_SECONDS ) ) {
// Notify the user they need to wait before redeeming another code
GFCommon::add_notification_message( $entry['id'], __( 'Please wait 24 hours before redeeming another enrollment code.', 'your-text-domain' ), 'warning' );
return false; // Prevent redemption
}
// If all checks pass, return the original $return_value (which is usually true by default)
return $return_value;
}
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/integrations/gravity-forms/class-gravity-forms-code-field.php:155
public static function gform_after_submission_func( $entry, $form ) {
// only run for logged in users
if ( ! is_user_logged_in() ) {
return;
}
$code_redeem_field = false;
$code_redemption = null;
foreach ( $form['fields'] as $field ) {
if ( 'uncanny_enrollment_code' !== $field->type ) {
continue;
}
$code_redeem_field = true;
$code_redemption = rgar( $entry, (string) $field->id );
break;
}
if ( false === $code_redeem_field ) {
return;
}
if ( empty( $code_redemption ) ) {
return;
}
$user_id = wp_get_current_user()->ID;
$coupon_id = Database::is_coupon_available( $code_redemption );
if ( true !== apply_filters( 'ulc_redeem_code_for_current_logged_in_user', true, $entry, $form, $user_id, $code_redemption, $coupon_id ) ) {
return;
}
if ( is_numeric( intval( $coupon_id ) ) ) {
update_user_meta( $user_id, Config::$uncanny_codes_tracking, 'Gravity Forms' );
$result = Database::set_user_to_coupon( $user_id, $coupon_id );
LearnDash::set_user_to_course_or_group( $user_id, $result );
do_action( 'ulc_user_redeemed_code', $user_id, $coupon_id, $result, 'gravityforms' );
}
}