The gform_post_add_subscription_payment action is triggered after a payment is made to an existing subscription in Gravity Forms.
Usage
add_action('gform_post_add_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_add_subscription_payment
Examples
Basic Usage
Create a function that does something when the action is triggered:
function my_function($entry, $action) {
// your custom code here
}
add_action('gform_post_add_subscription_payment', 'my_function', 10, 2);
Send an Email After Subscription Payment
Send an email to the admin after a subscription payment:
function send_email_after_payment($entry, $action) {
$to = '[email protected]';
$subject = 'New Subscription Payment';
$message = 'A new subscription payment has been made.';
wp_mail($to, $subject, $message);
}
add_action('gform_post_add_subscription_payment', 'send_email_after_payment', 10, 2);
Update User Meta After Subscription Payment
Update user meta after a subscription payment:
function update_user_meta_after_payment($entry, $action) {
$user_id = $entry['created_by'];
$payments = get_user_meta($user_id, 'subscription_payments', true);
$payments = $payments ? $payments + 1 : 1;
update_user_meta($user_id, 'subscription_payments', $payments);
}
add_action('gform_post_add_subscription_payment', 'update_user_meta_after_payment', 10, 2);
Add a Note to the Entry After Subscription Payment
Add a note to the entry after a subscription payment:
function add_note_after_payment($entry, $action) {
$note = 'A subscription payment was made on ' . current_time('mysql');
GFFormsModel::add_note($entry['id'], $entry['form_id'], $entry['created_by'], $note);
}
add_action('gform_post_add_subscription_payment', 'add_note_after_payment', 10, 2);
Log Subscription Payment
Log subscription payment information to a custom log file:
function log_subscription_payment($entry, $action) {
$log_message = 'Payment received: ' . $action['amount'] . ' - Transaction ID: ' . $action['transaction_id'];
$log_file = fopen(get_stylesheet_directory() . '/subscription_payments.log', 'a');
fwrite($log_file, $log_message . PHP_EOL);
fclose($log_file);
}
add_action('gform_post_add_subscription_payment', 'log_subscription_payment', 10, 2);