Displaying Total amount of a donation form With a Short Code
This concept focuses on developers implementing a system to track the total donation amount collected through a designated form. Users utilize this form to contribute predefined amounts, which are then processed and aggregated. The developer’s task involves creating a backend solution that accurately records each donation transaction, calculates the cumulative sum in real-time, and provides precise reporting of the total donated amount. Key aspects include ensuring secure transaction handling, integrating with payment processing services, and offering analytics to assess the performance of the donation form.
Total paid donation amount
This is a shortcode script that works with Fluent forms – donation script.
I use fluent snippet plugin for all my functions, JS and CSS needs
Place script in the functions.php or use your favorite code snippet plugin
V2 – new code @jon did a update and cleaned the coding.
function get_total_amount() {
global $wpdb;
// Specify the form ID
$form_id = 7;
// Query the wp_fluentform_transactions table for successful payments for the specific form ID
$results = $wpdb->get_results($wpdb->prepare("
SELECT payment_total
FROM {$wpdb->prefix}fluentform_transactions
WHERE payment_status = 'paid' AND form_id = %d
", $form_id));
// Check if there are any results
if (empty($results)) {
return '<p class="total">No payments found for this form.</p>';
}
$totalPaid = 0;
// Loop through the results and calculate the total paid
foreach ($results as $result) {
// Assuming payment_total is in cents, divide by 100 to get the correct amount
$totalPaid += (float) $result->payment_total / 100;
}
// Escape the total paid before outputting and handle formatting
return '<p class="total">Total Collected Payment:<span> £' . esc_html(number_format($totalPaid, 2)) . '</span></p>';
}
// Register shortcode to display the total amount
add_shortcode('total_paid', 'get_total_amount');
V1 – old code
function get_total_amount() {
$entries = $formApi->entries();
//change the 5 to your form ID number
$formApi = fluentFormApi('forms')->entryInstance($formId = 5);
$totalEntries = count($entries);
$totalPaid=0;
for($i=0; $i<$totalEntries; $i++){
if($entries['data'][$i]->payment_status === 'paid'){
$totalPaid+= (($entries['data'][$i]->payment_total)/100);
} }
if (is_int($totalPaid)) {
return '<p class="total">Total Collected Payment:' . '<span> $' . number_format($totalPaid, 2) . '.00' . '</span></p>';
}
// register shortcode
add_shortcode('total_paid', 'get_total_amount');
CSS
Customize the CSS to fit your needs.
.total {
color:#ccc;
font-weight:bold;
}
.total span {
color:#ff0000;
font-weight:bold;
}
To display the total amount donated via form 5 → change the form id to match your form ID.
Place the shortcode in the shortcode block. You can add a custom style to it as well.
[total_paid]
This script AS IS.
Notes:
This basic script, CSS style, and plugin are designed to function optimally assuming minimal interference from your theme or other plugins. If conflicts occur or further customization is needed, additional adjustments may be necessary. Please note that this script, CSS style, or plugin must be used AS IS.
Wp Scriptly
Facebook friend @jon cleaned the coding up. I posted v2 above v1.