As a software developer, primarily a web developer to be precise, you will come across different clients requirements that you will need to find a way of solving. In one of my recent web projects, I was tasked in re-building a non-profit based website called Grief Dialogues. If you have not worked for a charity or non-profit organization, I advise you should. There so much you can learn about humanity if you read content and not just concerned about building the website.
Donation
Donation is something that allows the non-profit organization to grow and still stay afloat, and in this case, I was tasked in using the famously known WpCharitable Plugin for WordPress. I can’t deny it; I am a massive fan of WordPress. It’s Open Source and Free, and most important, it is limited to your imagination.
Charitable – Donation Plugin
Charitable – Donation provides a free WordPress donation plugin with support for geolocation, simple updates, videos, user avatars, anonymous donations, MailChimp sync, and so much more. Although the plugin is free to use, most of the sweet features lie in the premium packages like syncing your donors to MailChimp, donation campaign updates, and much more.
Okay, let’s get to the main deal here 🙂
The Challenge
Charitable allows you to send your donors information to MailChimp or other similar services using the “Charitable – Newsletter Connect” extension plugin (not free), but it doesn’t do one critical thing – Send the amount donated by the donor to MailChimp. This information was vital for me because, with the donor amount in MailChimp, you can segment donors into different contributing groups and then send targeted campaigns to each group or segment. For example, donors that have donated over a $1000 within 2 months can get some exclusive deals, medals or even put into your site hall of fame.
The Solution
Fortunately, it is possible to get that information using hooks and filters of the plugin itself (thanks to hints from Eric at Charitable). In my case, I needed the total amount ever donated by the donor and also the current amount donated by the donor.
The donation amount can be gotten using the donor_id parameter as shown below:
$donor = new Charitable_Donor( $user['donor_id'] ); $donation = $donor->get_last_donation();
It is important that the $donation parameter is not null, which basically means the donor hasn’t made any donation yet. If $donation is not null, then you can get the most recent donation amount and total donor lifetime value like this:
$most_recent_donation = $donation->get_total_donation_amount( true ); $lifetime_value = $donor->get_user()->get_total_donated();
With those two variables, you can then pass it out to your other function for another use case. To use with MailChimp, you have to use the charitable_newsletter_connect_mailchimp_subscriber_data hook and pass the variable to your MailChimp merge fields.
The full code of how it works is shown below:
function ed_charitable_add_mailchimp_merge_field_data( $data, $user ) { if ( array_key_exists( 'donor_id', $user ) ) { $donor = new Charitable_Donor( $user['donor_id'] ); $donation = $donor->get_last_donation(); $most_recent_donation = $donation->get_total_donation_amount( true ); $lifetime_value = $donor->get_user()->get_total_donated(); $data['merge_fields']['MMERGE15'] = $most_recent_donation; $data['merge_fields']['MMERGE22'] = $lifetime_value ; } return $data; } add_filter( 'charitable_newsletter_connect_mailchimp_subscriber_data', 'ed_charitable_add_mailchimp_merge_field_data', 10, 2 );
You can see the full code here in Github.