Action Uncanny Redemption Codes

ulc_codes_group_generated

Fires after a code group is generated, providing the group ID and insertion status for further processing.

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

Description

Fires after a new batch of coupon codes is generated and inserted into the database. Developers can use this hook to perform actions based on the group ID and the number of successfully inserted codes. This hook is triggered after successful database operations for code batch creation and insertion.


Usage

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

Parameters

$group_id (mixed)
The ID of the newly created code group.
$inserted (mixed)
The `$group_id` parameter contains the unique identifier of the newly created code group.

Examples

<?php
/**
 * Example function to hook into the 'ulc_codes_group_generated' action.
 * This function demonstrates how to process the generated group ID and the number of inserted codes.
 * It might be used to log the event, trigger further actions, or update custom metadata.
 *
 * @param int $group_id The ID of the generated code group.
 * @param int $inserted The number of codes successfully inserted into the group.
 */
function my_uncanny_learndash_codes_group_generated_handler( $group_id, $inserted ) {
    // Ensure we have valid parameters before proceeding
    if ( ! is_numeric( $group_id ) || ! is_numeric( $inserted ) ) {
        error_log( 'Invalid parameters received for ulc_codes_group_generated hook.' );
        return;
    }

    // Example: Log the successful generation of a code group.
    // In a real-world scenario, you might want to use a more robust logging system.
    error_log( sprintf(
        'Uncanny LearnDash Codes: A new code group was generated. Group ID: %d, Codes inserted: %d',
        $group_id,
        $inserted
    ) );

    // Example: You could also add custom meta to the group if needed.
    // This is just a hypothetical example; the actual structure of code groups
    // would depend on the Uncanny LearnDash Codes plugin's implementation.
    //
    // if ( class_exists( 'UC_Code_Groups' ) ) { // Hypothetical check for plugin class
    //     $group_data = get_post_meta( $group_id, '_ulc_group_data', true ); // Hypothetical meta key
    //     if ( is_array( $group_data ) ) {
    //         $group_data['generated_by_custom_hook'] = true;
    //         update_post_meta( $group_id, '_ulc_group_data', $group_data );
    //     }
    // }

    // If this were a filter hook, you would return a modified value:
    // return $group_id;
}

// Hook the function to the 'ulc_codes_group_generated' action.
// The priority is set to 10 (default) and it accepts 2 arguments ($group_id, $inserted).
add_action( 'ulc_codes_group_generated', 'my_uncanny_learndash_codes_group_generated_handler', 10, 2 );

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/admin/class-generate-codes.php:365

}

		$group_id                = Database::add_code_group_batch( $data );
		$inserted                = Database::add_codes_to_batch( $group_id, $codes, $args );
		self::$num_coupons_added = $inserted;


		do_action( 'ulc_codes_group_generated', $group_id, $inserted );
		if ( (int) self::$num_coupons_added === (int) self::$num_codes_requested && empty( self::$rejected_batch_codes ) ) {
			$redirect_url = admin_url( 'admin.php?page=uncanny-learndash-codes' ) . "&msg=success&codes_generated={$inserted}&group_id={$group_id}";
			$redirect_url = apply_filters( 'ulc_codes_batch_redirect_url', $redirect_url, $group_id, $data );
			wp_safe_redirect( $redirect_url );
			exit();
		} else {
			add_action( 'admin_notices', array( __CLASS__, 'success_notice' ) );


Scroll to Top