Filter uncanny-automator-pro

uap_option_all_wc_get_countries

Filters the list of WooCommerce countries available in Ultimate Addons for WooCommerce subscriptions.

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

Description

Filters the list of WooCommerce countries for Uncanny Automator's WooCommerce Subscriptions integration. Developers can add, remove, or modify country options returned by WooCommerce to customize available choices in Automator triggers and actions. This hook fires after WooCommerce countries are retrieved.


Usage

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

Parameters

$options (mixed)
This parameter contains the array of country options that will be returned by the filter.

Return Value

The filtered value.


Examples

/**
 * Filter to modify the list of WooCommerce countries for Uncanny Automator Pro.
 *
 * This example adds a custom country to the list, perhaps for a specific region
 * or for testing purposes, before it's presented in Uncanny Automator's country selection fields.
 *
 * @param array $options The array of country options, each with 'text' and 'value'.
 * @return array The modified array of country options.
 */
add_filter( 'uap_option_all_wc_get_countries', function( $options ) {

	// Check if $options is an array before proceeding.
	if ( ! is_array( $options ) ) {
		return $options; // Return original if not an array
	}

	// Add a hypothetical custom country.
	// In a real scenario, you might dynamically fetch this or have a specific use case.
	$custom_country_name = 'Atlantis';
	$custom_country_code = 'AT';

	// Ensure we don't duplicate if it somehow exists.
	$country_exists = false;
	foreach ( $options as $option ) {
		if ( isset( $option['value'] ) && $option['value'] === $custom_country_code ) {
			$country_exists = true;
			break;
		}
	}

	if ( ! $country_exists ) {
		$options[] = array(
			'text'  => esc_html( $custom_country_name ), // Sanitize output
			'value' => sanitize_key( $custom_country_code ), // Sanitize value
		);
	}

	// You could also remove specific countries if needed.
	// For example, to remove 'United States':
	/*
	$options = array_filter( $options, function( $option ) {
		return isset( $option['value'] ) && $option['value'] !== 'US';
	} );
	// Re-index array after filtering
	$options = array_values( $options );
	*/


	return $options;

}, 10, 1 ); // 10 is the default priority, 1 is the number of arguments accepted.

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-subscription/fields/order-fields.php:98

public function get_countries() {

		if ( ! class_exists( 'WC_Countries' ) ) {
			return array();
		}

		$countries = ( new WC_Countries() )->get_countries();

		$options = array(
			array(
				'text'  => esc_html_x( 'Select country/region', 'WooCommerce Subscription', 'uncanny-automator-pro' ),
				'value' => '',
			),
		);

		foreach ( $countries as $country_code => $country_name ) {

			if ( ! empty( $country_name ) && ! empty( $country_code ) ) {
				$options[] = array(
					'text'  => $country_name,
					'value' => $country_code,
				);
			}
		}

		return apply_filters( 'uap_option_all_wc_get_countries', $options );
	}

Scroll to Top