Filter uncanny-automator-pro

uap_option_get_wc_products

Filters WooCommerce products for the AffiliateWP integration, allowing customization of the returned product data.

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

Description

This filter allows modification of the WooCommerce product options array before it's returned. Developers can use this to customize product selection, add custom product types, or influence how product data is presented to the user, particularly within the AffiliateWP integration of Uncanny Automator.


Usage

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

Parameters

$option (mixed)
This parameter is used to pass an `$option` array, which may contain arguments to further filter the WooCommerce products.

Return Value

The filtered value.


Examples

add_filter( 'uap_option_get_wc_products', 'my_custom_uap_filter_wc_products', 10, 1 );

/**
 * Filters WooCommerce products for Uncanny Automator Pro.
 *
 * This function demonstrates how to modify the list of WooCommerce products
 * returned by Uncanny Automator Pro, for example, to exclude certain products
 * or to add custom product data.
 *
 * @param array $wc_products The array of WooCommerce product data.
 * @return array The modified array of WooCommerce product data.
 */
function my_custom_uap_filter_wc_products( $wc_products ) {
	// Example: Exclude products with a specific tag (e.g., 'private').
	$private_tag_id = get_term_by( 'slug', 'private', 'product_tag' );

	if ( $private_tag_id ) {
		$private_tag_id = $private_tag_id->term_id;
		foreach ( $wc_products as $key => $product_data ) {
			if ( isset( $product_data['product_tag_ids'] ) && is_array( $product_data['product_tag_ids'] ) ) {
				if ( in_array( $private_tag_id, $product_data['product_tag_ids'] ) ) {
					unset( $wc_products[ $key ] );
				}
			}
		}
		// Re-index the array after unsetting elements
		$wc_products = array_values( $wc_products );
	}

	// Example: Add a custom field to each product if it doesn't exist.
	foreach ( $wc_products as &$product_data ) {
		// Let's say we want to add the product's SKU if it's not already present in the data.
		if ( ! isset( $product_data['sku'] ) && isset( $product_data['id'] ) ) {
			$product = wc_get_product( $product_data['id'] );
			if ( $product ) {
				$product_data['sku'] = $product->get_sku();
			}
		}
	}
	unset( $product_data ); // Unset the reference to avoid unexpected behavior.

	return $wc_products;
}

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/affiliate-wp/helpers/affwp-pro-helpers.php:68

public function get_wc_products( $label = null, $option_code = 'WCPRODUCTS', $args = array() ) {
		if ( ! $label ) {
			$label = __( 'Product', 'uncanny-automator-pro' );
		}

		$token        = key_exists( 'token', $args ) ? $args['token'] : false;
		$is_ajax      = key_exists( 'is_ajax', $args ) ? $args['is_ajax'] : false;
		$target_field = key_exists( 'target_field', $args ) ? $args['target_field'] : '';
		$end_point    = key_exists( 'endpoint', $args ) ? $args['endpoint'] : '';
		$any_option   = key_exists( 'any_option', $args ) ? $args['any_option'] : false;

		$args = array(
			'post_type'      => 'product',
			'posts_per_page' => 999,
			'orderby'        => 'title',
			'order'          => 'ASC',
			'post_status'    => 'publish',
		);

		$options = Automator()->helpers->recipe->options->wp_query( $args, $any_option, esc_attr__( 'Any product', 'uncanny-automator' ) );

		$option = array(
			'option_code'     => $option_code,
			'label'           => $label,
			'input_type'      => 'select',
			'required'        => true,
			'supports_tokens' => $token,
			'is_ajax'         => $is_ajax,
			'fill_values_in'  => $target_field,
			'endpoint'        => $end_point,
			'options'         => $options,
		);

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

Scroll to Top