Filter uncanny-automator-pro

automator_woocommerce_get_coupons

Filters the list of coupon codes available within the WooCommerce integration.

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

Description

Filters the array of WooCommerce coupon codes available for use in Uncanny Automator. Developers can modify this array to include custom coupon codes or exclude specific ones, offering granular control over automations that utilize coupons. This hook fires when fetching coupon data for automator recipes.


Usage

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

Return Value

The filtered value.


Examples

add_filter( 'automator_woocommerce_get_coupons', 'my_custom_automator_exclude_coupons', 10, 2 );

/**
 * Excludes specific WooCommerce coupons from being available in Uncanny Automator.
 *
 * @param array $coupon_codes An array of coupon codes where keys and values are the coupon titles.
 * @param mixed $helper       The helper object passed from Uncanny Automator.
 *
 * @return array The modified array of coupon codes.
 */
function my_custom_automator_exclude_coupons( $coupon_codes, $helper ) {
    // Define an array of coupon titles to exclude.
    $coupons_to_exclude = array(
        'Internal Use Only',
        'Team Discount 2024',
    );

    // Iterate through the coupons to exclude.
    foreach ( $coupons_to_exclude as $coupon_title ) {
        // If a coupon title exists in the list of available coupons, remove it.
        if ( isset( $coupon_codes[ $coupon_title ] ) ) {
            unset( $coupon_codes[ $coupon_title ] );
        }
    }

    // Return the filtered list of coupon codes.
    return $coupon_codes;
}

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:555
uncanny-automator-pro/src/integrations/woocommerce-subscription/fields/order-fields.php:133

public function get_coupons() {

		$coupon_codes = array();

		$coupon_codes['0'] = __( 'Select coupon', 'uncanny-automator-pro' );

		$coupon_posts = get_posts(
			array(
				'posts_per_page' => 9999999,
				'orderby'        => 'name',
				'order'          => 'asc',
				'post_type'      => 'shop_coupon',
				'post_status'    => 'publish',
			)
		);

		foreach ( $coupon_posts as $coupon_post ) {
			$coupon_codes[ $coupon_post->post_title ] = $coupon_post->post_title;
		}

		return apply_filters( 'automator_woocommerce_get_coupons', $coupon_codes, $this );
	}


Scroll to Top