uap_option_all_wc_coupons
Filters the list of all WooCommerce coupons available for use within the Ultimate Affiliate Pro plugin.
add_filter( 'uap_option_all_wc_coupons', $callback, 10, 1 );
Description
This filter hook allows developers to modify the list of all WooCommerce coupons available for use in Uncanny Automator Pro recipes. It fires when the coupon options are being prepared for a recipe trigger or action, enabling customization of the coupon selection dropdown. Developers can filter the `$option` parameter to add, remove, or alter the available coupons based on custom logic.
Usage
add_filter( 'uap_option_all_wc_coupons', 'your_function_name', 10, 1 );
Parameters
-
$option(mixed) - This parameter likely contains the current value of the option being filtered, which is used by the `all_wc_coupons` function to retrieve a list of WooCommerce coupons.
Return Value
The filtered value.
Examples
add_filter( 'uap_option_all_wc_coupons', 'my_custom_wc_coupon_filter', 10, 1 );
/**
* Example: Exclude specific WooCommerce coupons from Uncanny Automator's list.
*
* This function filters the list of WooCommerce coupons provided to Uncanny Automator
* and removes any coupons that have a specific meta key or ID, for example.
*
* @param array $option The current array of coupon options.
* @return array The filtered array of coupon options.
*/
function my_custom_wc_coupon_filter( $option ) {
// Ensure we are working with the expected structure.
if ( ! isset( $option['options'] ) || ! is_array( $option['options'] ) ) {
return $option;
}
$filtered_coupons = array();
foreach ( $option['options'] as $coupon_code => $coupon_data ) {
// Example: Exclude coupons with a specific meta key set to 'true'.
// You would need to fetch the coupon object to check its meta data.
// For simplicity in this example, let's assume coupon_data might contain
// or allow us to derive a way to check for exclusion.
// In a real scenario, you'd use wc_get_coupon_by_code() and get_post_meta().
// Let's simulate an exclusion based on the coupon code for demonstration.
// For instance, exclude any coupon code starting with "TEST_".
if ( strpos( $coupon_code, 'TEST_' ) === 0 ) {
continue; // Skip this coupon
}
// Add the coupon to our filtered list if it passes the exclusion criteria.
$filtered_coupons[ $coupon_code ] = $coupon_data;
}
// Update the options with the filtered coupons.
$option['options'] = $filtered_coupons;
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
uncanny-automator-pro/src/integrations/woocommerce/helpers/woocommerce-pro-helpers.php:1612
public function all_wc_coupons( $label = null, $option_code = 'WOOCOUPONS', $is_any = false ) {
if ( ! $label ) {
$label = esc_html_x( 'Coupon', 'WooCommerce', 'uncanny-automator-pro' );
}
$coupon_posts = get_posts(
array(
'posts_per_page' => 9999999,
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'shop_coupon',
'post_status' => 'publish',
)
);
$options = array();
if ( true === $is_any ) {
$options['-1'] = esc_html_x( 'Any coupon', 'WooCommerce', 'uncanny-automator-pro' );
}
foreach ( $coupon_posts as $coupon_post ) {
$options[ $coupon_post->ID ] = $coupon_post->post_title;
}
$option = array(
'option_code' => $option_code,
'label' => $label,
'input_type' => 'select',
'required' => true,
'options' => $options,
'relevant_tokens' => array(),
);
return apply_filters( 'uap_option_all_wc_coupons', $option );
}