arm_cancel_subscription
Fires after a user's subscription is successfully canceled, providing the user and plan IDs.
add_action( 'arm_cancel_subscription', $callback, 10, 2 );
Description
Fires after a user's membership plan is successfully cancelled within ARMember. Developers can use this hook to trigger custom logic, such as sending notifications, updating external systems, or granting/revoking other related access based on the cancelled plan. Both the user ID and plan ID are passed for detailed targeting.
Usage
add_action( 'arm_cancel_subscription', 'your_function_name', 10, 2 );
Parameters
-
$user_id(mixed) - This parameter contains the ID of the user whose subscription is being cancelled.
-
$plan_id(mixed) - This parameter contains the ID of the user whose subscription is being canceled.
Examples
<?php
/**
* Example callback function for the 'arm_cancel_subscription' action hook.
*
* This function demonstrates how to react when a user's subscription is cancelled.
* It might be used to send a notification email, update external systems,
* or trigger other post-cancellation processes.
*
* @param int $user_id The ID of the user whose subscription is being cancelled.
* @param int $plan_id The ID of the membership plan being cancelled.
*/
function my_custom_arm_cancel_subscription_handler( $user_id, $plan_id ) {
// Check if the user ID and plan ID are valid integers.
if ( ! is_numeric( $user_id ) || ! is_numeric( $plan_id ) ) {
// Log an error or handle invalid input appropriately.
error_log( "Invalid user ID or plan ID received in arm_cancel_subscription hook." );
return;
}
// Convert to integers for safety.
$user_id = intval( $user_id );
$plan_id = intval( $plan_id );
// Fetch user details.
$user_info = get_user_by( 'id', $user_id );
if ( ! $user_info ) {
error_log( "User with ID {$user_id} not found during subscription cancellation." );
return;
}
// Fetch plan details (assuming you have a way to get plan details from its ID).
// This is a placeholder; you'd likely fetch this from the ARMember plugin's data.
// For example: $plan_details = ARM_Subscription_Plans::get_plan_details( $plan_id );
$plan_name = "Membership Plan {$plan_id}"; // Placeholder
// Example: Send an email notification to the user.
$to = $user_info->user_email;
$subject = "Your subscription to {$plan_name} has been cancelled.";
$message = "Dear {$user_info->display_name},nn";
$message .= "This is to confirm that your subscription to {$plan_name} has been successfully cancelled.nn";
$message .= "If you have any questions or believe this was an error, please contact support.nn";
$message .= "Sincerely,nYour Website Team";
$headers = array( 'Content-Type: text/plain; charset=UTF-8' );
// wp_mail is the standard WordPress function for sending emails.
$email_sent = wp_mail( $to, $subject, $message, $headers );
if ( $email_sent ) {
// Log success or perform other actions.
error_log( "Cancellation notification email sent successfully to {$user_info->user_email} for plan {$plan_id}." );
} else {
// Log failure.
error_log( "Failed to send cancellation notification email to {$user_info->user_email} for plan {$plan_id}." );
}
// Example: Update an external CRM or system.
// This is a conceptual example. Replace with actual API calls or integrations.
// if ( class_exists( 'My_CRM_Integration' ) ) {
// $crm = new My_CRM_Integration();
// $crm->update_subscription_status( $user_id, $plan_id, 'cancelled' );
// }
// You could also update custom user meta here if needed, beyond what ARMember does.
// update_user_meta( $user_id, 'last_subscription_cancellation_date', current_time( 'mysql' ) );
}
// Add the action with a priority (e.g., 10) and specify that the callback accepts 2 arguments.
add_action( 'arm_cancel_subscription', 'my_custom_arm_cancel_subscription_handler', 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
src/integrations/armember/actions/armember-membership-plan-cancelled.php:84
uncanny-automator-pro/src/integrations/armember/actions/armember-remove-from-membership-plan.php:143
protected function process_action( $user_id, $action_data, $recipe_id, $args, $parsed ) {
$plan_id = isset( $parsed[ $this->get_action_meta() ] ) ? sanitize_text_field( $parsed[ $this->get_action_meta() ] ) : '';
if ( empty( $plan_id ) ) {
$action_data['complete_with_errors'] = true;
$message = esc_html__( 'Plan does not exist.', 'uncanny-automator' );
Automator()->complete->action( $user_id, $action_data, $recipe_id, $message );
return;
}
global $arm_subscription_plans;
do_action( 'arm_before_update_user_subscription', $user_id, '0' );
$arm_subscription_plans->arm_add_membership_history( $user_id, $plan_id, 'cancel_subscription' );
do_action( 'arm_cancel_subscription', $user_id, $plan_id );
$arm_subscription_plans->arm_clear_user_plan_detail( $user_id, $plan_id );
update_user_meta( $user_id, 'arm_secondary_status', 6 );
Automator()->complete->action( $user_id, $action_data, $recipe_id );
}