Filter
uncanny-automator-pro
uap_option_all_edd_discount_codes
Filters all Easy Digital Downloads discount codes available for user account options, allowing modification before display.
add_filter( 'uap_option_all_edd_discount_codes', $callback, 10, 1 );
Description
Filters the options array for selecting all Easy Digital Downloads discount codes. Developers can use this hook to modify the available discount code options, add custom validation, or alter relevant tokens before they are presented in Uncanny Automator. Fires when retrieving discount code options for a recipe.
Usage
add_filter( 'uap_option_all_edd_discount_codes', 'your_function_name', 10, 1 );
Parameters
-
$option(mixed) - This parameter holds the pre-selected value or option for the discount code.
Return Value
The filtered value.
Examples
/**
* Filters the list of all Easy Digital Downloads discount codes.
*
* This function might be used to modify the list of discount codes before they are displayed
* in a Uncanny Automator select field, for instance, to exclude certain codes or to
* prepend custom information to the code names.
*
* @param array $option An array containing the discount codes, typically in a format suitable for a select field.
* Expected structure might include 'options' and 'current_value'.
* @return array The modified array of discount codes.
*/
add_filter( 'uap_option_all_edd_discount_codes', function( $option ) {
// Ensure we have a valid structure to work with.
if ( ! is_array( $option ) || ! isset( $option['options'] ) || ! is_array( $option['options'] ) ) {
return $option;
}
// Example: Let's say we want to prefix all discount codes with "UA_PREFIX_"
$modified_discount_codes = array();
foreach ( $option['options'] as $code_key => $code_value ) {
// Assuming $code_value is the actual discount code string.
// If it's an array, adjust accordingly. For this example, we assume string.
if ( is_string( $code_value ) ) {
$modified_discount_codes[ $code_key ] = 'UA_PREFIX_' . $code_value;
} else {
// If the value is not a string (e.g., an array of discount code details),
// we'll keep it as is or adapt the logic. For simplicity here, we keep it.
$modified_discount_codes[ $code_key ] = $code_value;
}
}
// Update the 'options' part of the array with our modified codes.
$option['options'] = $modified_discount_codes;
// You might also want to adjust the 'current_value' if it exists and
// needs to reflect the modified codes, though this is less common.
// For example, if the current value is "DISCOUNT10" and we're prefixing,
// you might want to set current_value to "UA_PREFIX_DISCOUNT10".
if ( isset( $option['current_value'] ) && is_string( $option['current_value'] ) ) {
$prefix = 'UA_PREFIX_';
if ( strpos( $option['current_value'], $prefix ) !== 0 ) {
$option['current_value'] = $prefix . $option['current_value'];
}
}
return $option;
}, 10, 1 ); // Priority 10, accepts 1 argument
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
uncanny-automator-pro/src/integrations/edd/helpers/edd-pro-helpers.php:69
public function all_edd_discount_codes( $label = null, $option_code = 'EDDDISCOUNTCODES', $any_option = true ) {
if ( ! $label ) {
$label = esc_attr_x( 'Discount code', 'Edd', 'uncanny-automator' );
}
$all_discount_codes = array();
if ( true === $any_option ) {
$all_discount_codes[] = array(
'text' => esc_attr_x( 'Any discount code', 'Edd', 'uncanny-automator' ),
'value' => '-1',
);
}
$discounts = edd_get_discounts();
if ( isset( $discounts ) && ! empty( $discounts ) ) {
foreach ( $discounts as $discount ) {
$title = $discount->post_title;
// set up a descriptive title for posts with no title.
if ( empty( $title ) ) {
/* translators: ID of recipe, trigger or action */
$title = sprintf( esc_attr_x( 'ID: %1$s (no title)', 'Edd', 'uncanny-automator' ), $discount->ID );
}
// add post as an option.
$all_discount_codes[] = array(
'text' => $title,
'value' => $discount->ID,
);
}
}
$option = array(
'option_code' => $option_code,
'label' => $label,
'input_type' => 'select',
'required' => true,
// to setup example, lets define the value the child will be based on
'current_value' => false,
'validation_type' => 'text',
'options' => $all_discount_codes,
'relevant_tokens' => array(),
);
return apply_filters( 'uap_option_all_edd_discount_codes', $option );
}