Solved: How to automatically deactivate a coupon after successful use?
I'm trying to automatically deactivate a coupon after it has been successfully used in a form, but my code snippet isn't working as expected. I hope someone here might have an idea.
My Goal:
When a customer uses a one-time coupon in my booking form and completes the payment, I want the status of that coupon to be automatically changed from 'active' to 'inactive' in the database to prevent it from being used again.
My Approach:
I'm using the fluentform/after_transaction_status_change hook. My logic is to get the coupon code from the submitted form data and then run a $wpdb->update query on the wp_fluentform_coupons table.
Here is the exact snippet I am using in WP Codebox:
PHP
<?php
/**
* Deactivates a coupon after it has been used in the booking form.
*/
add_action('fluentform/after_transaction_status_change', function ($new_status, $submission, $transaction_id) {
// --- Configuration ---
// The ID of my booking form
$booking_form_id = 99; // I have set my correct form ID here
// The name attribute of my coupon field
$coupon_field_name = 'coupon';
// ---------------------
// Only run for the booking form and on successful payment
if ($submission->form_id != $booking_form_id || $new_status !== 'paid') {
return;
}
$formData = (array) $submission->response;
// Check if a coupon was used in this submission
if (!empty($formData[$coupon_field_name])) {
$used_coupon_code = sanitize_text_field($formData[$coupon_field_name]);
$table_name = $GLOBALS['wpdb']->prefix . 'fluentform_coupons';
// This is the part that should deactivate the coupon
$GLOBALS['wpdb']->update(
$table_name,
['status' => 'inactive'], // The new data
['code' => $used_coupon_code] // The WHERE clause
);
}
}, 10, 3);
The Problem:
The form submission and payment work perfectly. The customer gets the discount. However, the coupon's status in the wp_fluentform_coupons table remains 'active'. The UPDATE query does not seem to have any effect.
My Question:
Is fluentform/after_transaction_status_change the correct hook to use for updating the database after a transaction? Is it possible that the action runs in a context where $wpdb->update might fail silently? Or is there a better, more reliable hook for deactivating a coupon after it has been successfully redeemed?
Any ideas or examples would be greatly appreciated!
Thank you, Manuel
Hi can you open a support ticket here - https://wpmanageninja.com/account/support-tickets/
our support engineers will look into it. They might need to run test and need more context.
Thanks
The support team kindly sent me a snippet that solves my problem exactly. Thank you very much.
Here is the snippet:
<?php
add_action('fluentform/submission_inserted', function($entryId, $formData, $form) {
$booking_form_id = 3; // Replace with your form ID
$coupon_field_name = 'payment-coupon'; // Replace with your coupon field name attribute
if ($form->id != $booking_form_id) {
return;
}
if (!empty($formData\[$coupon_field_name])) {
$used_coupon_code = sanitize_text_field($formData\[$coupon_field_name]);
global $wpdb;
$table_name = $wpdb->prefix . 'fluentform_coupons';
$coupon = $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM $table_name WHERE code = %s AND status = %s",
$used_coupon_code,
'active'
)
);
if ($coupon) {
$updated = $wpdb->update(
$table_name,
\['status' => 'inactive'],
\['code' => $used_coupon_code]
);
<?php
add_action('fluentform/submission_inserted', function($entryId, $formData, $form) {
$booking_form_id = 3; // Replace with your form ID
$coupon_field_name = 'payment-coupon'; // Replace with your coupon field name attribute
if ($form->id != $booking_form_id) {
return;
}
if (!empty($formData[$coupon_field_name])) {
$used_coupon_code = sanitize_text_field($formData[$coupon_field_name]);
global $wpdb;
$table_name = $wpdb->prefix . 'fluentform_coupons';
$coupon = $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM $table_name WHERE code = %s AND status = %s",
$used_coupon_code,
'active'
)
);
if ($coupon) {
$updated = $wpdb->update(
$table_name,
['status' => 'inactive'],
['code' => $used_coupon_code]
);
if ($updated === false) {
error_log("Coupon update failed for code '{$used_coupon_code}': " . $wpdb->last_error);
} else {
error_log("Coupon '{$used_coupon_code}' deactivated successfully after form submission.");
}
} else {
error_log("Coupon '{$used_coupon_code}' is either already inactive or does not exist.");
}
}
}, 10, 3);