The gform_post_fail_subscription_payment action is triggered after an existing subscription payment fails in Gravity Forms.
Usage
add_action('gform_post_fail_subscription_payment', 'my_function', 10, 2);
Parameters
- $entry (Entry Object) – The entry object that was created.
- $action (array) – The action that occurred within the subscription payment. Contains further information about the subscription.
$action = array( 'type' => '', 'amount' => '', 'transaction_type' => '', 'transaction_id' => '', 'subscription_id' => '', 'entry_id' => '', 'payment_status' => '', 'note' => '', );
More information
See Gravity Forms Docs: gform_post_fail_subscription_payment
Examples
Basic Usage
Create a function that does something when a subscription payment fails:
function my_function() {
// Do something here
}
add_action('gform_post_fail_subscription_payment', 'my_function', 10, 2);
Update User Role
Update the user role when a subscription payment fails:
add_action('gform_post_fail_subscription_payment', 'update_user_role');
function update_user_role($entry) {
if (function_exists('gf_user_registration')) {
// Use WP_User to change role - https://developer.wordpress.org/reference/classes/wp_user/
$user = gf_user_registration()->get_user_by_entry_id($entry['id']);
if (is_object($user)) {
$user->remove_role('role_one');
$user->add_role('role_two');
}
}
}
Send Notification Email
Send a notification email to the user when a subscription payment fails:
add_action('gform_post_fail_subscription_payment', 'send_notification_email', 10, 2);
function send_notification_email($entry, $action) {
$user_email = $entry['3']; // Replace '3' with the field ID of the email field in your form
$subject = 'Subscription Payment Failed';
$message = 'We noticed that your subscription payment failed. Please update your payment information.';
wp_mail($user_email, $subject, $message);
}
Add a Note to the Entry
Add a note to the entry when a subscription payment fails:
add_action('gform_post_fail_subscription_payment', 'add_fail_payment_note', 10, 2);
function add_fail_payment_note($entry, $action) {
$note = 'Subscription payment failed. Transaction ID: ' . $action['transaction_id'];
GFFormsModel::add_note($entry['id'], 'payment', '0', $note);
}
Log Failed Payment
Log the failed payment details when a subscription payment fails:
add_action('gform_post_fail_subscription_payment', 'log_failed_payment', 10, 2);
function log_failed_payment($entry, $action) {
$log_message = 'Failed Payment - Entry ID: ' . $entry['id'] . ', Transaction ID: ' . $action['transaction_id'];
error_log($log_message);
}