Filter uncanny-automator

automator_uo_codes_generate_group_data

Filters the group data generated for Uncanny Codes automations before it's saved or used.

add_filter( 'automator_uo_codes_generate_group_data', $callback, 10, 2 );

Description

Fires after the initial group data is prepared for generating Uncanny Codes. Developers can filter and modify this data, for instance, to dynamically set expiry dates or usage limits for generated code groups, before they are saved to the database.


Usage

add_filter( 'automator_uo_codes_generate_group_data', 'your_function_name', 10, 2 );

Parameters

$data (mixed)
This parameter contains the data that will be used to generate the group for Uncanny Codes.
$this (mixed)
This parameter contains the group data that will be used to generate the codes.

Return Value

The filtered value.


Examples

/**
 * Example: Modify the data array before Uncanny Codes generates a new group of codes.
 * This could be used to dynamically set a coupon's max usage based on other recipe data.
 *
 * @param array $data The original data array for the code group.
 * @param object $this The current instance of the UC_Generate_Codes action.
 * @return array The modified data array.
 */
add_filter( 'automator_uo_codes_generate_group_data', function( $data, $action_instance ) {

    // Assume we want to dynamically set 'coupon-max-usage' based on another piece of data
    // available in the action instance, for example, a meta field called 'max_uses_from_recipe'.
    // In a real scenario, you would retrieve this data more robustly.

    // Let's pretend $action_instance->meta contains recipe-specific settings.
    // This is a simplified example and actual meta retrieval might differ.
    $recipe_specific_max_uses = isset( $action_instance->meta['max_uses_from_recipe'] ) ? intval( $action_instance->meta['max_uses_from_recipe'] ) : 0;

    // If a specific max usage is defined in the recipe meta and it's a positive number,
    // override the default 'coupon-max-usage'.
    if ( $recipe_specific_max_uses > 0 ) {
        $data['coupon-max-usage'] = $recipe_specific_max_uses;
    }

    // You could also modify other parameters like expiry dates or character types.
    // For example, to always set expiry to 30 days from today:
    /*
    $today = new DateTime();
    $today->add( new DateInterval( 'P30D' ) ); // Add 30 days
    $data['expiry-date'] = $today->format('Y-m-d');
    $data['expiry-time'] = $today->format('H:i:s');
    */

    return $data;
}, 10, 2 ); // Priority 10, accepts 2 arguments ($data, $this)

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/integrations/uncanny-codes/actions/uc-generate-codes.php:148

public function generate_codes( $user_id, $action_data, $recipe_id, $args ) {

		$batch_name         = Automator()->parse->text( $action_data['meta']['UCBATCHNAME'], $recipe_id, $user_id, $args );
		$no_of_codes        = Automator()->parse->text( $action_data['meta']['UCNOOFCODES'], $recipe_id, $user_id, $args );
		$no_of_use_per_code = Automator()->parse->text( $action_data['meta']['UCUSERPERCODE'], $recipe_id, $user_id, $args );
		$expiry_date        = Automator()->parse->text( $action_data['meta']['UCEXPIRYDATE'], $recipe_id, $user_id, $args );
		$expiry_time        = Automator()->parse->text( $action_data['meta']['UCEXPIRYTIME'], $recipe_id, $user_id, $args );
		$prefix             = Automator()->parse->text( $action_data['meta']['UCPREFIX'], $recipe_id, $user_id, $args );
		$suffix             = Automator()->parse->text( $action_data['meta']['UCSUFFIX'], $recipe_id, $user_id, $args );
		$character_type     = array( 'uppercase-letters', 'numbers' );
		$codes              = array();

		$args = array(
			'generation_type' => 'auto',
			'coupon_amount'   => $no_of_codes,
			'custom_codes'    => '',
			'dashes'          => array( 4, 4, 4, 4, 4 ),
			'prefix'          => $prefix,
			'suffix'          => $suffix,
			'code_length'     => 20,
			'character_type'  => $character_type,
		);

		// Sanitize values
		$data = array(
			'coupon-amount'         => $no_of_codes,
			'coupon-prefix'         => $prefix,
			'coupon-suffix'         => $suffix,
			'coupon-dash'           => '4-4-4-4-4',
			'coupon-length'         => '20',
			'generation-type'       => 'auto',
			'dependency'            => 'automator',
			'coupon-for'            => 'automator',
			'group-name'            => $batch_name,
			'coupon-courses'        => '',
			'coupon-group'          => '',
			'expiry-date'           => $expiry_date,
			'expiry-time'           => $expiry_time,
			'coupon-paid-unpaid'    => 'default',
			'coupon-max-usage'      => $no_of_use_per_code,
			'coupon-character-type' => $character_type,
		);

		$data     = apply_filters( 'automator_uo_codes_generate_group_data', $data, $this );
		$group_id = Database::add_code_group_batch( $data );
		$inserted = Database::add_codes_to_batch( $group_id, $codes, $args );

		if ( 0 === $inserted && $inserted !== $no_of_codes ) {
			$error_message = esc_attr__( 'Something went wrong! Codes not generated, Try again.', 'uncanny-automator' );
			Automator()->complete_action( $user_id, $action_data, $recipe_id, $error_message );

			return;
		}

		$this->hydrate_tokens(
			array(
				'BATCH_ID'        => $group_id,
				'CODES_GENERATED' => $this->get_generated_codes( $group_id ),
			)
		);

		Automator()->complete_action( $user_id, $action_data, $recipe_id );

	}

Scroll to Top