Using Gravity Forms ‘gform_subscription_canceled’ PHP action

The gform_subscription_canceled action hook allows you to perform custom actions when a subscription has been canceled.

Usage

add_action('gform_subscription_canceled', 'your_function_name', 10, 3);

Parameters

  • $entry (Entry Object): The entry from which the canceled subscription was originally generated.
  • $feed (Feed Object): The feed from which the subscription was originally generated.
  • $transaction_id (string): The transaction ID of the canceled subscription.

More information

See Gravity Forms Docs: gform_subscription_canceled

Examples

Run Custom Function

Update your order on a fictional third party order fulfillment service.

add_action('gform_subscription_canceled', 'remove_user_privileges', 10, 3);

function remove_user_privileges($entry, $feed, $transaction_id) {
    global $wpdb;

    // Get user id by querying for the entry id in the user meta
    $sql = $wpdb->prepare("select user_id from wp_usermeta where meta_key = 'entry_id' and meta_value = {$entry['id']}");
    $user_id = $wpdb->get_var($sql);

    // Use function to remove privileges for a user from a fictional third party application
    mtp_remove_privileges($user_id, $transaction_id);
}

Downgrade User Role

Downgrade the user role when a subscription is canceled via one of the credit card based payment add-ons.
Note: This code requires User Registration version 3.0+.

add_action('gform_subscription_canceled', 'downgrade_user_role', 10, 2);

function downgrade_user_role($entry, $feed) {
    if (rgar($feed, 'addon_slug') != 'gravityformspaypal' && function_exists('gf_user_registration')) {
        $user = gf_user_registration()->get_user_by_entry_id($entry['id']);
        if (!empty($user) && !is_wp_error($user)) {
            $user->set_role('pastsubscriber');
        }
    }
}

Mailchimp – Unsubscribe Member

Unsubscribe a member from a Mailchimp list when the subscription is canceled. This example requires Gravity Forms Mailchimp add-on version 4.0 or greater. It also requires that the entry which created the subscription was the same entry which subscribed the user to the Mailchimp list.

add_action('gform_subscription_canceled', function($entry) {
    // Your custom code here
});

Placement
This code should be placed in the functions.php file of your active theme.

Source Code

do_action('gform_subscription_canceled', $entry, $feed, $transaction_id);

This action hook is located in GFPaymentAddOn::cancel_subscription() in includes/addon/class-gf-payment-addon.php.