Filter uncanny-automator

automator_mepr_renewal_completed_is_not_first_real_payment

Filters whether a MemberPress renewal is a subscriber's first real payment, not a trial or initial purchase.

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

Description

Filters whether a completed MemberPress recurring subscription renewal is considered a non-initial real payment. Developers can use this to conditionally trigger automations based on whether the renewal is the user's first *actual* recurring payment beyond the initial signup.


Usage

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

Parameters

$subscription (mixed)
This parameter holds the MemberPress subscription object associated with the renewal.
$event (mixed)
This parameter represents the MemberPress subscription object associated with the renewal event.

Return Value

The filtered value.


Examples

<?php
/**
 * Example filter for automator_mepr_renewal_completed_is_not_first_real_payment.
 *
 * This filter allows customizing the logic to determine if a completed renewal
 * is NOT the first real payment for a subscription.
 *
 * @param bool $is_not_first_real_payment The default calculated value.
 * @param object $event The event object.
 * @return bool True if it's not the first real payment, false otherwise.
 */
add_filter( 'automator_mepr_renewal_completed_is_not_first_real_payment', function( $is_not_first_real_payment, $event ) {
    // In this example, we'll consider a renewal as NOT the first real payment
    // if it's not a "lifetime" product AND the default calculation says it's a renewal.
    // We also want to ensure the subscription has completed at least 2 payments.

    $transaction = $event->get_data();
    if ( ! $transaction instanceof MeprTransaction ) {
        return $is_not_first_real_payment; // Return default if data is not a MeprTransaction
    }

    $subscription = $transaction->subscription();
    if ( ! $subscription instanceof MeprSubscription ) {
        return $is_not_first_real_payment; // Return default if subscription is not found
    }

    $product = $transaction->product();
    if ( ! $product instanceof MeprProduct ) {
        return $is_not_first_real_payment; // Return default if product is not found
    }

    // Check if it's a lifetime product, if so, it can't be a "renewal" in the typical sense.
    if ( 'lifetime' === (string) $product->period_type ) {
        return false; // Lifetime products are never considered renewals for this logic.
    }

    // Check if the subscription has completed more than one payment.
    // MeprSubscription::completed_transactions() returns an array of completed transactions.
    $completed_transactions = $subscription->completed_transactions();

    // If there's more than one completed transaction, it means this is a renewal.
    if ( is_array( $completed_transactions ) && count( $completed_transactions ) > 1 ) {
        return true; // This is a renewal and not the first payment.
    }

    // If none of the above conditions are met, fall back to the default logic.
    return $is_not_first_real_payment;

}, 10, 2 ); // Priority 10, accepts 2 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

src/integrations/memberpress/triggers/mp-renews-recurring-subscription.php:94

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

		/** @var MeprTransaction $transaction */
		$transaction = $event->get_data();
		/** @var MeprProduct $product */
		$product                   = $transaction->product();
		$subscription              = $transaction->subscription();
		$is_not_first_real_payment = apply_filters(
			'automator_mepr_renewal_completed_is_not_first_real_payment',
			Automator()->helpers->recipe->memberpress->check_if_is_renewal_or_first_payment( $subscription ),
			$event
		);

		if ( 'lifetime' !== (string) $product->period_type && false === $is_not_first_real_payment ) {
			return true;
		}

		return false;
	}

Scroll to Top