Filter uncanny-automator-pro

uap_option_all_wc_product_categories

Filters the list of all WooCommerce product categories available for selection within the plugin's options.

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

Description

Filters the WooCommerce product category options array. Developers can modify or add to the available product categories, or customize options for 'supports_custom_value' and 'custom_value_description' when this hook fires, typically when generating dynamic dropdowns for Uncanny Automator recipes.


Usage

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

Parameters

$option (mixed)
This parameter represents the current value of the option being filtered.

Return Value

The filtered value.


Examples

add_filter(
	'uap_option_all_wc_product_categories',
	function( $option ) {
		// Example: Add a custom option to include WooCommerce product categories.
		// This might be useful if you want to trigger an automation based on
		// a specific category being added to a product, or if you need to
		// select a category for a trigger or action.

		// Check if the current option is intended for product categories.
		// In a real scenario, you'd likely have more sophisticated logic
		// to identify the specific context of this option.
		if ( isset( $option['value'] ) && is_array( $option['value'] ) && count( $option['value'] ) > 0 ) {

			// Let's assume we want to add a "None" option at the beginning.
			$none_option = array(
				'label' => esc_html__( 'Select a category...', 'your-text-domain' ),
				'value' => '', // Empty value for "none"
			);

			// Prepend the "none" option to the existing categories.
			array_unshift( $option['value'], $none_option );

			// Example: If the option supports custom values, ensure the description is clear.
			if ( isset( $option['supports_custom_value'] ) && true === $option['supports_custom_value'] ) {
				$option['custom_value_description'] = esc_html__( 'Enter a specific WooCommerce Product Category ID.', 'your-text-domain' );
			}
		}

		return $option;
	},
	10, // Priority
	1  // Accepted arguments
);

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:800

public function all_wc_product_categories( $label = null, $option_code = 'WOOPRODCAT', $args = array() ) {

		$supports_multiple_values = key_exists( 'supports_multiple_values', $args ) ? $args['supports_multiple_values'] : false;
		$description              = key_exists( 'description', $args ) ? $args['description'] : false;
		$required                 = key_exists( 'required', $args ) ? $args['required'] : true;
		if ( ! $label ) {
			$label = esc_html_x( 'Categories', 'WooCommerce', 'uncanny-automator-pro' );
		}

		global $wpdb;

		$categories = $wpdb->get_results(
			$wpdb->prepare(
				"SELECT terms.term_id,terms.name FROM $wpdb->terms as terms
				LEFT JOIN $wpdb->term_taxonomy as rel ON (terms.term_id = rel.term_id)
				WHERE rel.taxonomy = %s
				ORDER BY terms.name",
				'product_cat'
			)
		);

		$options       = array();
		$options['-1'] = esc_html_x( 'Any category', 'WooCommerce', 'uncanny-automator-pro' );

		foreach ( $categories as $category ) {
			$title = $category->name;
			if ( empty( $title ) ) {
				// translators: %1$s is the category ID
				$title = sprintf( esc_html_x( 'ID: %1$s (no title)', 'WooCommerce', 'uncanny-automator-pro' ), $category->term_id );
			}
			$options[ $category->term_id ] = $title;
		}

		$option = array(
			'option_code'              => $option_code,
			'label'                    => $label,
			'description'              => $description,
			'input_type'               => 'select',
			'required'                 => $required,
			'options'                  => $options,
			'supports_multiple_values' => $supports_multiple_values,
			'relevant_tokens'          => array(
				$option_code          => esc_html_x( 'Category title', 'WooCommerce', 'uncanny-automator-pro' ),
				$option_code . '_ID'  => esc_html_x( 'Category ID', 'WooCommerce', 'uncanny-automator-pro' ),
				$option_code . '_URL' => esc_html_x( 'Category URL', 'WooCommerce', 'uncanny-automator-pro' ),
			),
		);

		// Support custom token value.
		if ( isset( $args['supports_custom_value'] ) && true === $args['supports_custom_value'] ) {
			$option['supports_custom_value']    = true;
			$option['custom_value_description'] = isset( $args['custom_value_description'] )
				? $args['custom_value_description']
				: esc_html_x( 'Product category ID', 'WooCommerce', 'uncanny-automator-pro' );
		}

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

Scroll to Top