Filter uncanny-automator-pro

automator_pro_mp_coupon_redeemed_validate_txn_status

Filters a transaction's status to validate it when a MemberPress coupon is redeemed.

add_filter( 'automator_pro_mp_coupon_redeemed_validate_txn_status', $callback, 10, 2 );

Description

Filters whether a MemberPress coupon redemption transaction should proceed. Developers can return `true` to enforce stricter validation (requiring a completed transaction) or `false` to bypass this check, allowing incomplete transactions to trigger automations. Default is `false`.


Usage

add_filter( 'automator_pro_mp_coupon_redeemed_validate_txn_status', 'your_function_name', 10, 2 );

Parameters

$transaction (mixed)
This parameter is a placeholder, initially set to `false`, and is not actively used in the function's logic as the first parameter is immediately reassigned from the `$args` array.
$coupon (mixed)
This parameter contains the MemberPress transaction object that was just redeemed.

Return Value

The filtered value.


Examples

add_filter( 'automator_pro_mp_coupon_redeemed_validate_txn_status', 'my_custom_mp_coupon_redeemed_validation', 10, 3 );

/**
 * Custom validation for MemberPress coupon redeemed trigger.
 *
 * This function adds a custom check to ensure that only completed
 * MemberPress transactions are considered valid when a coupon is redeemed.
 * It overrides the default behavior if the filter is set to `true`.
 *
 * @param bool        $validate_txn_status  The current validation status (default: false).
 * @param MeprTransaction $transaction The MemberPress transaction object.
 * @param object      $coupon        The MemberPress coupon object.
 *
 * @return bool True if the transaction status is valid, false otherwise.
 */
function my_custom_mp_coupon_redeemed_validation( $validate_txn_status, $transaction, $coupon ) {

    // If the default validation is already true, we don't need to do anything.
    if ( true === $validate_txn_status ) {
        return true;
    }

    // Enforce that the transaction must be in a 'complete' status for automation.
    // This prevents automations from firing on pending or failed transactions.
    if ( $transaction->status === MeprTransaction::$complete_str ) {
        return true; // Allow the transaction to proceed with validation.
    }

    // If the transaction status is not 'complete', return false to prevent the automation.
    return false;
}

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/memberpress/triggers/mp-coupon-redeemed.php:125

protected function validate_trigger( ...$args ) {
		list( $transaction ) = array_shift( $args );

		if ( ! $transaction instanceof MeprTransaction ) {
			return false;
		}

		/** @var MeprTransaction $transaction */
		if ( empty( $transaction->coupon() ) ) {
			return false;
		}

		$coupon = $transaction->coupon();

		$validate_txn_status = apply_filters( 'automator_pro_mp_coupon_redeemed_validate_txn_status', false, $transaction, $coupon );

		if ( true === $validate_txn_status && $transaction->status != MeprTransaction::$complete_str ) {
			return false;
		}

		return true;
	}

Scroll to Top