Home / Admin / Update Donation Summary
Duplicate Snippet

Embed Snippet on Your Site

Update Donation Summary

Often you might want to change the output of where a donation summary is outputted for a campaign (which also can exist in the campaigns shortcode). The below is a "bare bones" example that would likely require some design and CSS updates but shows a %, donation amount, and a number of donors - perfect update for a campaign "card" on the campaigns shortcode area or similar shortcodes in a custom theme.

Code Preview
php
<?php
add_filter('charitable_donation_summary', 'charitable_custom_donation_summary', 10, 4 );
/**
 * Custom donation summary output.
 *
 * @param string $ret The default donation summary output.
 * @param Charitable_Campaign $campaign The campaign object.
 * @param int $amount The amount raised.
 * @param int $goal The campaign goal.
 *
 * @return string
 */
function charitable_custom_donation_summary( $ret, $campaign, $amount = 0, $goal = 0 ) {
	global $wp, $post;
	if ( is_admin() ) {
		return $ret;
	}
	// You may not want to run this on all pages where a donation summary is shown on the frontend.
	// So you can put a check here if (for example) you want this only to appear on a page where you are using the campaigns shortcode.
	if ( is_singular( 'campaign' ) ) {
		return $ret;
	}
	$currency_helper = charitable_get_currency_helper();
	// get the percent raised from amount and goal.
	$percent = 0;
	if ( $goal ) {
		$percent = ( $amount / $goal ) * 100;
	}
	// round percent to the nearest whole number.
	$percent = round( $percent );
	$amount_raised = $currency_helper->get_monetary_amount( $amount );
	$donor_count = $campaign->get_donor_count();
	// generate the output HTML, basic for this example.
	$html = '<div>Percent Raised: ' . $percent . '%</div>';
	$html .= '<div>Amount Raised: ' . $amount_raised . '</div>';
	$html .= '<div>Donor Count: ' . $donor_count . '</div>';
	return $html;
}

Comments

Add a Comment