ulc_code_max_usage
Filters the maximum usage limit for a specific code group before it is applied.
add_filter( 'ulc_code_max_usage', $callback, 10, 3 );
Description
Filters the maximum usage count for individual coupon codes within a group. Developers can use this to override the default maximum usage set per code, allowing for more flexible coupon management. The hook passes the current maximum usage, the code's purpose ('automator' or 'manual'), and the group object for context.
Usage
add_filter( 'ulc_code_max_usage', 'your_function_name', 10, 3 );
Parameters
-
$group(mixed) - This parameter represents the current group being processed, likely an object containing group details.
-
$group(mixed) - This parameter represents the group object currently being processed, containing information about its settings and properties.
-
$group(mixed) - This parameter appears to be a placeholder for the `$group` object itself, potentially passed multiple times within the hook's execution context.
Return Value
The filtered value.
Examples
/**
* Example: Modify the maximum usage for automator codes if a certain condition is met.
*
* This filter is applied when the maximum usage per code is initially set to 1
* and the code is intended for an 'automator' type. This allows for overriding
* the default maximum usage based on specific group properties or other logic.
*
* @param int $max_per_code The current maximum usage per code.
* @param string $code_for The type of code ('automator', etc.).
* @param int $group_id The ID of the code group.
*
* @return int The modified maximum usage per code.
*/
add_filter(
'ulc_code_max_usage',
function ( $max_per_code, $code_for, $group_id ) {
// Let's assume we want to allow 5 uses per code for 'automator' types
// if the group has a specific meta key set, for demonstration purposes.
$group = get_post( $group_id ); // Re-fetch the group object for meta access
if ( 'automator' === $code_for && $group && 'automator_special_batch' === get_post_meta( $group_id, '_ulc_special_batch_type', true ) ) {
// If it's an 'automator' code and belongs to a special batch,
// allow a higher usage limit, for example, 5 uses per code.
return 5;
}
// Otherwise, return the original $max_per_code value.
return $max_per_code;
},
10, // Priority
3 // Accepted arguments
);
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-view-groups.php:193
src/classes/helpers/class-database.php:599
}
break;
case 'used_count':
$used = Database::get_group_redeemed_count( $group->ID );
$issue = absint( $total_codes_count );
$max_per_code = (int) $group->issue_max_count;
if ( 1 === $max_per_code && 'automator' === (string) $group->code_for ) {
$max_per_code = (int) apply_filters( 'ulc_code_max_usage', $group->issue_max_count, $group->code_for, $group->ID );
}
$usage_text = absint( $used ) . ' / ' . absint( $issue * $max_per_code );
$tooltip_text = sprintf(
esc_html__( 'Total codes: %d | Max uses per code: %d', 'uncanny-learndash-codes' ),
$issue,
$max_per_code
);