Filter uncanny-automator-pro

automator_wcs_validate_trigger_code_pieces

Filters the trigger code pieces for WooCommerce Subscriptions actions and renewals before validation.

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

Description

Filters the WooCommerce trigger code pieces for validation. Developers can use this hook to modify or add to the array of trigger code pieces before they are validated, allowing for custom WooCommerce trigger logic within Uncanny Automator.


Usage

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

Parameters

$args (mixed)
This parameter contains an array of specific WooCommerce Subscription trigger codes that are being validated.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of how to hook into 'automator_wcs_validate_trigger_code_pieces' to add a new trigger code for validation.
 *
 * This filter allows you to extend the list of valid trigger codes that Uncanny Automator's WooCommerce Subscriptions integration
 * will process. In this example, we're adding a hypothetical trigger code 'MY_CUSTOM_SUBSCRIPTION_EVENT' to the list.
 *
 * @param array $valid_trigger_codes An array of existing valid trigger codes.
 * @param mixed $args The arguments passed to the filter.
 * @return array The modified array of valid trigger codes.
 */
add_filter( 'automator_wcs_validate_trigger_code_pieces', function( $valid_trigger_codes, $args ) {

	// Add a new custom trigger code to the array of valid codes.
	// This would typically be used if you've created a custom WooCommerce Subscriptions trigger
	// that Uncanny Automator should recognize.
	$valid_trigger_codes[] = 'MY_CUSTOM_SUBSCRIPTION_EVENT';

	// You could also add conditional logic here. For instance, if $args contains specific data,
	// you might choose to add or remove trigger codes based on that.
	// Example:
	// if ( isset( $args['some_conditional_data'] ) && $args['some_conditional_data'] === true ) {
	//     $valid_trigger_codes[] = 'ANOTHER_CONDITIONAL_TRIGGER';
	// }

	return $valid_trigger_codes;
}, 10, 2 );

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/tokens/wc-pro-tokens.php:438

public function save_token_data( $args, $trigger ) {
		if ( ! isset( $args['trigger_args'] ) || ! isset( $args['entry_args']['code'] ) ) {
			return;
		}

		$trigger_meta = $args['entry_args']['meta'];
		$trigger_code = $args['entry_args']['code'];

		if ( 'WCSUBSCRIPTIONSSWITCHED' === $trigger_code ) {
			/**
			 * @var WC_Order        $order
			 * @var WC_Subscription $subscription
			 */
			list( $order, $subscription, $add_line_item, $remove_line_item ) = $args['trigger_args'];
			$trigger_log_entry                                               = $args['trigger_entry'];
			if ( ! empty( $subscription ) ) {
				Automator()->db->token->save( 'subscription_id', $subscription->get_id(), $trigger_log_entry );
				Automator()->db->token->save( 'order_id', $order->get_id(), $trigger_log_entry );

				$product_id        = wc_get_order_item_meta( $add_line_item, '_product_id', true );
				$variation_id_from = wc_get_order_item_meta( $remove_line_item, '_variation_id', true );
				$variation_id_to   = wc_get_order_item_meta( $add_line_item, '_variation_id', true );
				Automator()->db->token->save( "{$trigger_meta}_FROM", $variation_id_from, $trigger_log_entry );
				Automator()->db->token->save( "{$trigger_meta}_TO", $variation_id_to, $trigger_log_entry );
				Automator()->db->token->save( $trigger_meta, $product_id, $trigger_log_entry );
			}
		}

		$trigger_meta_validations = apply_filters(
			'automator_wcs_validate_trigger_code_pieces',
			array( 'WCS_PAYMENT_FAILS', 'WC_SUBSCRIPTION_RENEWAL_COUNT' ),
			$args
		);

		/** @var mixed $trigger_meta_validations */
		if ( in_array( $args['entry_args']['code'], $trigger_meta_validations, true ) ) {
			$subscription      = $args['trigger_args'][0];
			$trigger_log_entry = $args['trigger_entry'];
			if ( ! empty( $subscription ) && $subscription instanceof WC_Subscription ) {
				Automator()->db->token->save( 'subscription_id', $subscription->get_id(), $trigger_log_entry );
				Automator()->db->token->save( 'order_id', $subscription->get_parent_id(), $trigger_log_entry );
			}
		}

	}

Scroll to Top