Filter uncanny-automator

uap_option_get_all_code_batch

Filters all options for retrieving coupon codes in batches before they are returned by the Uncanny Codes plugin.

add_filter( 'uap_option_get_all_code_batch', $callback, 10, 1 );

Description

Filters all available Uncanny Codes batch options before they are retrieved. Developers can use this to add, modify, or remove batch options from the list returned by the Uncanny Codes integration. This hook is called internally when fetching batch data.


Usage

add_filter( 'uap_option_get_all_code_batch', 'your_function_name', 10, 1 );

Parameters

$option (mixed)
This parameter holds the database query results containing all available Uncanny Codes batch options.

Return Value

The filtered value.


Examples

/**
 * Adds custom batch options to the Uncanny Codes batch selection.
 *
 * This filter allows developers to modify or extend the list of available
 * Uncanny Codes batches that can be selected within the Uncanny Automator plugin.
 *
 * @param array $option The current array of options for batch selection.
 *                      Expected format: [ 'batch_id' => 'Batch Name' ].
 *
 * @return array The modified array of options for batch selection.
 */
add_filter( 'uap_option_get_all_code_batch', 'my_custom_uncanny_codes_batches', 10, 1 );

function my_custom_uncanny_codes_batches( $option ) {
	// Example: Add a special "Global Discount" batch if it doesn't already exist.
	// In a real scenario, you might fetch this from another source or based on specific conditions.
	$global_discount_id = 'global_discount_batch';
	$global_discount_name = esc_attr__( 'Global Discount Code', 'my-text-domain' );

	// Check if this special batch is already in the options.
	if ( ! array_key_exists( $global_discount_id, $option ) ) {
		// Add the new batch to the existing options.
		$option[ $global_discount_id ] = $global_discount_name;
	}

	// You could also conditionally remove existing batches if needed.
	// For instance, if a certain batch should not be available under specific circumstances.
	// if ( isset( $option['some_specific_batch_id'] ) ) {
	//     unset( $option['some_specific_batch_id'] );
	// }

	// Always return the modified options array.
	return $option;
}

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/helpers/uncanny-codes-helpers.php:190
src/integrations/uncanny-codes/helpers/uncanny-codes-helpers.php:201

public function get_all_code_batch( $label = null, $option_code = 'UCBATCH', $is_any = false ) {

		if ( ! $label ) {
			$label = esc_attr__( 'Prefix', 'uncanny-automator' );
		}

		global $wpdb;

		$options = array();
		if ( $is_any ) {
			$options['-1'] = esc_html__( 'Any batch', 'uncanny-automator' );
		}
		$option      = array(
			'option_code'     => $option_code,
			'label'           => $label,
			'input_type'      => 'select',
			'required'        => true,
			'options'         => $options,
			'relevant_tokens' => array(
				$option_code              => esc_attr__( 'Batch', 'uncanny-automator' ),
				'UNCANNYCODESBATCHEXPIRY' => esc_attr__( 'Batch expiry date', 'uncanny-automator' ),
			),
		);
		$all_batches = $wpdb->get_results( 'SELECT DISTINCT id, name FROM ' . $wpdb->prefix . 'uncanny_codes_groups', ARRAY_A );

		if ( empty( $all_batches ) ) {
			return apply_filters( 'uap_option_get_all_code_batch', $option );
		}
		foreach ( $all_batches as $batch ) {
			if ( ! empty( $batch['name'] ) ) {
				$options[ $batch['id'] ] = $batch['name'];
			}
		}

		natcasesort( $options );
		$option['options'] = $options;

		return apply_filters( 'uap_option_get_all_code_batch', $option );
	}

Scroll to Top