PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Show total Donations

  1. Hi I have created a site for a friend that was injured in Afganistan http://www.a-soldiers-journey.co.uk He is doing things for Charity and I have created a donations form, I am new to wordpress and was wondering if someone can show me how to add a running total of all donations that updates dynamically each time someone donates money through paypal Thank you in advance Alistair

    Posted 12 years ago on Tuesday January 17, 2012 | Permalink
  2. I created a donation form like yours and embedded it here:
    http://gravity.chrishajer.com/make-a-donation/

    You can make a donation there. It is just a fake submission.

    If you check the left sidebar, there is a widget at the bottom that holds the running total of donations. Check it before and after making a donation.

    The widget holds this text:

    [php]
    [donations form=78]<br />
    
    <a href="http://gravity.chrishajer.com/make-a-donation/" title="make a donation">Make a donation today</a>

    I added this code to my theme's functions.php to retrieve all donations from one form, form 78 in my case: http://pastebin.com/qCWZfhdW

    The code is commented. You will need to change the [3] in line 17 to your field ID (I believe it is field 5 in your donation form.) And when you use the shortcode, it will probably look like this, since I think yours is form 2:

    [php]
    [donations form=2]

    The last line of that paste is required to process shortcodes in widgets, which WordPress does not do by default. You may or may not need that line, depending on where you insert this shortcode to show the total, and if your theme is already configured to process shortcodes in widgets. Try the code without it first, and if you don't see the output, but just the shortcode in brackets, then you need that line.

    Please let us know if you have any questions.

    Thanks.

    Posted 12 years ago on Wednesday January 18, 2012 | Permalink
  3. Thanks for the reply Chris it is working to a point but the ammount isnt just showing as GBP0.00

    My Short code is :

    <span style="font-size:16px; color:#069; font-weight:bold;">Amount Raised</span>
    <span style=" font-size:14px; color:#999; font-weight:bold;">[donations form=2]</span>

    and my functions.php file I have added at the bottom:

    // http://pastebin.com/kHpaHQvi originally
    // http://www.gravityhelp.com/forums/topic/show-total-donations
    // usage: [donations form=2] where 37 is the form ID
    add_shortcode('donations', 'total_donations');
    function total_donations($atts) {
            $form_id = $atts['form'];
            // function to pull all the entries for one form
            $donations = RGFormsModel::get_leads($form_id);
            // start the total at zero
            $total = 0;
            // initialize a counter for the number of donations made
            $i = 0;
            // loop through all the returned results
            foreach ($donations as $amount) {
                    // add each donation amount to the total
                    // change 3 here to the field ID which holds the donation amount
                    $total += $amount[5];
                    // increment the counter so we know how many donations there are as well
                    $i++;
            }
            // change this to your locale, or, if your locale is already defined, you can remove this line
            setlocale(LC_MONETARY, 'en_GB');
            // do some formatting and return the html output from the shortcode
            $output = "We have raised " . money_format( '%i', $total) . " from $i donors.";
            // just the string above will be returned.  You can style it here or where you are using the shortcode
            return $output;
    }
    // needed for the above to process the donation shortcode in sidebar widget
    add_filter('widget_text', 'do_shortcode');

    What have I done wrong thanks Alistair

    Posted 12 years ago on Thursday January 19, 2012 | Permalink
  4. Sounds like field 5 is not the field that is holding the donation amounts? Can you export the form and email it to me at chris@rocketgenius.com and I will take a look at it for you? It's probably something simple.

    Can you verify that the donation amounts are being stored with the entries?

    Posted 12 years ago on Friday January 20, 2012 | Permalink
  5. The problem was that the donation amount was being stored as a string, with the GBP as part of the donation in your case. That made the math fail. So I added a bit of code to strip non-numeric characters from the string, then add it. See this:

    Be sure to change the field ID back if it's not correct for your form. I believe field 5 is correct though, on line 18. Your shortcode should not need any modification.

    http://pastebin.com/cee0wXZ6

    I also sent you an email.

    Posted 12 years ago on Saturday January 21, 2012 | Permalink
  6. Thanks Chris works a Treat just one thing can i change the GBP to £ instead

    Posted 12 years ago on Monday January 23, 2012 | Permalink