Filter uncanny-automator-pro

automator_pro_woocommerce_use_user_id_instead_wc_user_id

Filters to control if a user ID from WooCommerce should be used instead of the provided user ID.

add_filter( 'automator_pro_woocommerce_use_user_id_instead_wc_user_id', $callback, 10, 3 );

Description

Filters whether to use the WordPress user ID (`$user_id`) instead of the WooCommerce customer ID (`$wc_user_id`) when looking up a customer. Developers can return `true` to enforce the use of `$user_id` in specific scenarios, particularly when `$wc_user_id` is `0` or the lookup fails. This hook is applied after initial checks and before the final customer ID is determined.


Usage

add_filter( 'automator_pro_woocommerce_use_user_id_instead_wc_user_id', 'your_function_name', 10, 3 );

Parameters

$wc_user_id (mixed)
This parameter represents the default value of `false`, indicating whether to use the `$user_id` instead of the `$wc_user_id` when the WooCommerce user ID is not found or is zero.
$user_id (mixed)
This parameter represents the WooCommerce user ID, which might be an integer or false if no associated user is found.
$billing_email (mixed)
This parameter contains the WordPress user ID, which can be used as an alternative to the WooCommerce user ID if certain conditions are met or if the filter allows.

Return Value

The filtered value.


Examples

/**
 * Example of how to use the 'automator_pro_woocommerce_use_user_id_instead_wc_user_id' filter.
 *
 * This filter allows you to force the use of the WordPress user ID ($user_id)
 * as the WooCommerce user ID ($wc_user_id) in specific scenarios, even if
 * WooCommerce might have otherwise determined a different ID or no ID.
 *
 * For instance, you might want to always use the logged-in user's ID
 * when they are performing an action within Uncanny Automator, regardless
 * of whether their billing email resolves to a WooCommerce customer.
 */
add_filter(
	'automator_pro_woocommerce_use_user_id_instead_wc_user_id',
	function ( $use_old_logic, $wc_user_id, $user_id, $billing_email ) {
		// In this example, we want to always prioritize the WordPress user ID ($user_id)
		// if it's a valid, non-zero ID, and if the original $wc_user_id was not found (is 0).
		// This effectively forces Uncanny Automator to use the logged-in user's ID
		// if WooCommerce couldn't find a specific customer associated with the order/action.

		// Check if we have a valid WordPress user ID and if the original WooCommerce user ID was not found.
		if ( is_numeric( $user_id ) && $user_id > 0 && ( $wc_user_id === 0 || false === $wc_user_id ) ) {
			// Return true to indicate that we want to use the $user_id instead of the $wc_user_id.
			return true;
		}

		// If the above condition is not met, we return the original value of $use_old_logic.
		// This ensures that the default behavior of the filter is maintained for other cases.
		return $use_old_logic;
	},
	10, // Priority
	4  // 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:1247

}
		if ( false === $wc_user_id ) {
			$wc_user_id = 0;
		}

		// Only override wc_user_id with user_id if billing email lookup failed or wc_user_id is 0
		// Allow filtering to restore old logic if needed
		$use_old_logic = apply_filters( 'automator_pro_woocommerce_use_user_id_instead_wc_user_id', false, $wc_user_id, $user_id, $billing_email );

		if ( ( $use_old_logic || 0 === $wc_user_id ) && is_numeric( $user_id ) && 0 !== $user_id ) {
			// Use user_id if:
			// - Old logic is enabled via filter, OR
			// - Billing email lookup failed (wc_user_id = 0)
			$wc_user_id = $user_id;
		}

Scroll to Top