Filter
uncanny-automator-pro
automator_pro_edd_cancel_subscription_by_id_can_cancel
Filters if an Easy Digital Downloads subscription can be canceled by its ID before the cancellation process.
add_filter( 'automator_pro_edd_cancel_subscription_by_id_can_cancel', $callback, 10, 3 );
Description
Allows developers to control whether a specific EDD subscription can be canceled. Filter the `true` value to `false` to prevent cancellation based on subscription ID, subscription object, or other custom logic. This hook fires before the subscription cancellation process begins.
Usage
add_filter( 'automator_pro_edd_cancel_subscription_by_id_can_cancel', 'your_function_name', 10, 3 );
Parameters
-
$subscription_id(mixed) - This parameter is a boolean value that indicates whether the subscription can be canceled.
-
$subscription(mixed) - This parameter contains the ID of the Easy Digital Downloads subscription to be canceled.
-
$this(mixed) - This parameter holds the subscription object fetched from Easy Digital Downloads.
Return Value
The filtered value.
Examples
/**
* Prevent cancellation of subscriptions older than a certain date.
*
* @param bool $can_cancel The current cancel status.
* @param int $subscription_id The ID of the subscription.
* @param object $subscription The subscription object.
* @param object $automator_action The Automator Pro action object.
*
* @return bool Whether the subscription can be canceled.
*/
add_filter( 'automator_pro_edd_cancel_subscription_by_id_can_cancel', function( $can_cancel, $subscription_id, $subscription, $automator_action ) {
// Define the cutoff date (e.g., 1 year ago)
$cutoff_date = strtotime( '-1 year' );
// Get the subscription creation date
$subscription_creation_date = strtotime( $subscription->date_created );
// If the subscription was created more than a year ago, prevent cancellation
if ( $subscription_creation_date < $cutoff_date ) {
return false; // Prevent cancellation
}
// Otherwise, allow cancellation
return $can_cancel;
}, 10, 4 );
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/edd-recurring/actions/edd-cancel-subscription-by-id.php:94
protected function process_action( $user_id, $action_data, $recipe_id, $args, $parsed ) {
// Get the selected subscription ID
$subscription_id = sanitize_text_field( $parsed[ $this->get_action_meta() ] );
if ( empty( $subscription_id ) ) {
$this->add_log_error( esc_html_x( 'Please enter a valid subscription ID.', 'EDD - Recurring Payments', 'uncanny-automator-pro' ) );
return false;
}
$subscription = new EDD_Subscription( $subscription_id );
if ( ! $subscription ) {
$this->add_log_error(
sprintf(
// translators: %1$s: Subscription ID
esc_html_x( 'The provided ID %1$s did not return a valid Subscription.', 'EDD - Recurring Payments', 'uncanny-automator-pro' ),
$subscription_id
)
);
return false;
}
if ( false === $subscription->can_cancel() ) {
$this->add_log_error(
sprintf(
// translators: %1$s: Subscription ID
esc_html_x( 'The subscription %1$s is noncancellable.', 'EDD - Recurring Payments', 'uncanny-automator-pro' ),
$subscription->id
)
);
return false;
}
if ( false === apply_filters( 'automator_pro_edd_cancel_subscription_by_id_can_cancel', true, $subscription_id, $subscription, $this ) ) {
$this->add_log_error(
sprintf(
// translators: %1$s: Subscription ID
esc_html_x( 'You are not allowed to cancel this subscription %1$s.', 'EDD - Recurring Payments', 'uncanny-automator-pro' ),
$subscription->id
)
);
return false;
}
$subscription->cancel();
return true;
}