Action
uncanny-automator-pro
arm_cancel_subscription_gateway_action
Fires when a subscription is successfully cancelled by a payment gateway.
add_action( 'arm_cancel_subscription_gateway_action', $callback, 10, 2 );
Description
Fired when a user's subscription to a recurring plan is canceled within armember. Developers can use this hook to trigger custom actions, such as revoking access to content, updating user roles, or logging cancellation events, after the subscription has been processed by the payment gateway.
Usage
add_action( 'arm_cancel_subscription_gateway_action', '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 cancelled.
Examples
/**
* Example of how to hook into the arm_cancel_subscription_gateway_action.
*
* This function will be triggered when a subscription is canceled through an ARMember gateway.
* It logs the cancellation event and potentially performs additional gateway-specific actions.
*
* @param int|string $user_id The ID of the user whose subscription is being canceled.
* @param int|string $plan_id The ID of the membership plan being canceled.
*/
function my_custom_arm_cancel_subscription_gateway( $user_id, $plan_id ) {
// Ensure we have valid user and plan IDs.
if ( ! empty( $user_id ) && ! empty( $plan_id ) ) {
// Fetch user object to get more details if needed.
$user = get_user_by( 'id', $user_id );
if ( $user ) {
// Get ARMember plan object for further checks or data retrieval.
// This assumes you have ARMember core classes available.
global $arm_membersip_plans;
if ( ! isset( $arm_membersip_plans ) ) {
$arm_membersip_plans = new ARM_Membership_Plans();
}
$plan_obj = $arm_membersip_plans->arm_get_plan_details( $plan_id );
if ( $plan_obj && $plan_obj['is_recurring'] ) {
// Log the cancellation to a custom table or file for auditing.
error_log( "ARMember Subscription Cancellation: User ID {$user_id} ({$user->user_login}) canceled plan ID {$plan_id} ({$plan_obj['name']})." );
// --- Gateway-Specific Logic Example ---
// If you were integrating with a custom payment gateway that needs to be notified,
// you would add that logic here.
// For example, if you had a class that handles your gateway:
/*
if ( class_exists( 'My_Custom_Gateway' ) ) {
$gateway_handler = new My_Custom_Gateway();
$gateway_handler->handle_subscription_cancellation( $user_id, $plan_id );
}
*/
// --- End Gateway-Specific Logic Example ---
} else {
// Log if the plan wasn't recurring, though this hook usually fires for recurring.
error_log( "ARMember Subscription Cancellation: Attempted to cancel non-recurring plan ID {$plan_id} for user ID {$user_id}." );
}
} else {
error_log( "ARMember Subscription Cancellation: User ID {$user_id} not found." );
}
} else {
error_log( "ARMember Subscription Cancellation: Invalid user ID or plan ID received." );
}
}
add_action( 'arm_cancel_subscription_gateway_action', 'my_custom_arm_cancel_subscription_gateway', 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/armember/actions/armember-remove-from-membership-plan.php:139
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 = __( 'Plan does not exist.', 'uncanny-automator-pro' );
Automator()->complete->action( $user_id, $action_data, $recipe_id, $message );
return;
}
$arm_subscription_plans = $this->armember_subscription_class;
$old_plan_ids = get_user_meta( $user_id, 'arm_user_plan_ids', true );
if ( empty( $old_plan_ids ) ) {
$action_data['do-nothing'] = true;
$action_data['complete_with_errors'] = true;
$message = __( 'The user is not a member of any membership plan.', 'uncanny-automator-pro' );
Automator()->complete->action( $user_id, $action_data, $recipe_id, $message );
return;
}
if ( ! empty( $old_plan_ids ) && ! in_array( $plan_id, $old_plan_ids, true ) ) {
$action_data['do-nothing'] = true;
$action_data['complete_with_errors'] = true;
$message = sprintf( __( 'The user is not a member of %s.', 'uncanny-automator-pro' ), $arm_subscription_plans->arm_get_plan_name_by_id( $plan_id ) );
Automator()->complete->action( $user_id, $action_data, $recipe_id, $message );
return;
}
$planData = get_user_meta( $user_id, 'arm_user_plan_' . $plan_id, true );
$plan_detail = $planData['arm_current_plan_detail'];
$planData['arm_cencelled_plan'] = 'yes';
update_user_meta( $user_id, 'arm_user_plan_' . $plan_id, $planData );
if ( ! empty( $plan_detail ) ) {
$planObj = ! class_exists( 'ARM_Plan' ) ? new ARM_Plan_Lite( 0 ) : new ARM_Plan( 0 );
$planObj->init( (object) $plan_detail );
} else {
$planObj = ! class_exists( 'ARM_Plan' ) ? new ARM_Plan_Lite( $plan_id ) : new ARM_Plan( $plan_id );
}
if ( $planObj->exists() && $planObj->is_recurring() ) {
do_action( 'arm_cancel_subscription_gateway_action', $user_id, $plan_id );
}
$arm_subscription_plans->arm_add_membership_history( $user_id, $plan_id, 'cancel_subscription', array(), 'admin' );
do_action( 'arm_cancel_subscription', $user_id, $plan_id );
$arm_subscription_plans->arm_clear_user_plan_detail( $user_id, $plan_id );
$user_future_plans = get_user_meta( $user_id, 'arm_user_future_plan_ids', true );
$user_future_plans = ! empty( $user_future_plans ) ? $user_future_plans : array();
if ( ! empty( $user_future_plans ) ) {
if ( in_array( $plan_id, $user_future_plans ) ) {
unset( $user_future_plans[ array_search( $plan_id, $user_future_plans ) ] );
update_user_meta( $user_id, 'arm_user_future_plan_ids', array_values( $user_future_plans ) );
}
}
Automator()->complete->action( $user_id, $action_data, $recipe_id );
}