Sovled. Help Needed: Custom function for fluentform/payment_success is not running
Hi everyone,
I'm trying to automatically generate a unique coupon after a successful payment, but my custom function doesn't seem to be running at all, and I'm hoping someone can spot what I'm doing wrong.
My Goal: When a customer buys a product from my form (ID 5), I want to create a unique coupon with the value of that product.
What I've Tried: I'm using the following PHP code, which includes error_log statements for debugging. My form ID is 5, and the payment field's name attribute is payment_input.
PHP
<?php
add_action('fluentform/payment_success', function ($entryId, $formData, $form, $paymentData) {
// --- My Configuration ---
$target_form_id = 5;
$payment_field_name = 'payment_input';
// -------------------------
error_log('--- Fluent Form Payment Hook started for Form ID: ' . $form->id . ' ---');
// Only run for the target form
if ($form->id != $target_form_id) {
error_log('ERROR: Hook triggered for the wrong form ID: ' . $form->id);
return;
}
error_log('INFO: Correct form ID (' . $target_form_id . ') detected.');
// Check if the payment field has data
if (empty($formData[$payment_field_name]) || !is_array($formData[$payment_field_name])) {
error_log('ERROR: Payment field "' . $payment_field_name . '" not found or is empty.');
error_log('INFO: Available form data keys: ' . print_r(array_keys($formData), true));
return;
}
error_log('INFO: Payment field "' . $payment_field_name . '" found.');
// Loop through each purchased item
foreach ($formData[$payment_field_name] as $item) {
error_log('INFO: Processing item: ' . print_r($item, true));
$product_value = floatval($item['value']);
if ($product_value <= 0) {
continue;
}
// Generate the code and insert into the database...
// ... (rest of the coupon creation logic)
}
}, 10, 4);
// ... (helper function to generate the code is also included)
The Problem:
I have placed the code in [PLEASE SPECIFY HERE: for example, "my active child theme's functions.php file" or "the Code Snippets plugin and the snippet is active"].
- I run a test transaction with Stripe.
- The payment is successful, and the entry status in Fluent Forms correctly changes to "Paid".
- I have enabled
WP_DEBUGandWP_DEBUG_LOGin mywp-config.phpfile. - However, absolutely none of the
error_logmessages from my script appear in the/wp-content/debug.logfile. The log file only shows unrelated notices from other plugins.
This tells me that the function is not being executed at all. The fluentform/payment_success action is not being triggered for my code.
My Question:
How does this really work? Can anyone see what I'm doing wrong?
- Is there a common mistake in how or where this kind of code should be placed that would prevent it from running?
- Is there a known issue with the
fluentform/payment_successhook that I should be aware of? - What is the definitive, correct way to implement a function that should trigger after a successful payment?
I feel like I'm missing one crucial step, and I'd be very grateful for any advice or insight.
Thanks in advance!
We hope you are doing well today. It appears you are not using the correct hook name. Please use 'fluentform/form_payment_success' as referenced inΒ Documentation.
Thank you for your reply.
Thanks to Taylor, I now have a working code ;)
If anyone needs it, just copy it ;)
<?php
add_action('fluentform/after_transaction_status_change', function ($new_status, $submission, $transaction_id) {
// --- Konfiguration ---
$target_form_ids = [5];
$payment_field_name = 'payment_input';
$hidden_field_name = 'generierter_gutscheincode'; // Der Name Ihres Hidden Fields
// ---------------------
if (!in_array($submission->form_id, $target_form_ids) || $new_status !== 'paid') {
return;
}
$formData = (array) $submission->response;
if (empty($formData[$payment_field_name])) {
return;
}
$customer_email = !empty($formData['email']) ? sanitize_email($formData['email']) : null;
$purchased_item_labels = $formData[$payment_field_name];
$form_structure = fluentFormApi('forms')->find($submission->form_id);
$form_fields = json_decode($form_structure->form_fields, true);
$pricing_options = [];
foreach ($form_fields['fields'] as $field) {
if (isset($field['attributes']['name']) && $field['attributes']['name'] === $payment_field_name) {
if (isset($field['settings']['pricing_options'])) {
$pricing_options = $field['settings']['pricing_options'];
break;
}
}
}
if (empty($pricing_options)) return;
foreach ($purchased_item_labels as $purchased_label) {
foreach ($pricing_options as $option) {
if (trim($option['label']) === trim($purchased_label)) {
$product_value = floatval($option['value']);
if ($product_value > 0) {
$coupon_code = ff_generate_final_coupon_code_v7();
$table_name = $GLOBALS['wpdb']->prefix . 'fluentform_coupons';
$temp_title = "{$customer_email} - {$purchased_label}";
$coupon_settings = [ 'coupon_limit' => 1 ];
$coupon_data = [
'title' => $temp_title,
'code' => $coupon_code,
'coupon_type' => 'fixed',
'amount' => $product_value,
'status' => 'active',
'settings' => serialize($coupon_settings),
'created_at' => current_time('mysql'),
'updated_at' => current_time('mysql')
];
$GLOBALS['wpdb']->insert($table_name, $coupon_data);
$new_coupon_id = $GLOBALS['wpdb']->insert_id;
if ($new_coupon_id) {
$final_title = "#{$new_coupon_id} - {$temp_title}";
$GLOBALS['wpdb']->update(
$table_name,
['title' => $final_title],
['id' => $new_coupon_id]
);
update_submission_with_coupon_v7($submission->id, $hidden_field_name, $coupon_code);
}
}
break;
}
}
}
}, 10, 3);
function update_submission_with_coupon_v7($submission_id, $field_name, $coupon_code) {
global $wpdb;
$table_name = $wpdb->prefix . 'fluentform_submissions';
$submission = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name WHERE id = %d", $submission_id));
if (!$submission) {
return;
}
$response_data = json_decode($submission->response, true);
$response_data[$field_name] = $coupon_code;
$new_response_json = json_encode($response_data);
$wpdb->update(
$table_name,
['response' => $new_response_json],
['id' => $submission_id],
['%s'],
['%d']
);
}
function ff_generate_final_coupon_code_v7($length = 10) {
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-!_?';
$code = '';
$max = strlen($chars) - 1;
for ($i = 0; $i < $length; $i++) { $code .= $chars[random_int(0, $max)]; }
$table_name = $GLOBALS['wpdb']->prefix . 'fluentform_coupons';
$existing_code = $GLOBALS['wpdb']->get_var($GLOBALS['wpdb']->prepare("SELECT code FROM $table_name WHERE code = %s", $code));
if ($existing_code) {
return ff_generate_final_coupon_code_v7($length);
}
return $code;
}