Skip to content

Instantly share code, notes, and snippets.

@ericnicolaas
Last active February 4, 2019 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericnicolaas/7cc99114413aee1d0ac8d7ec05e4a1ad to your computer and use it in GitHub Desktop.
Save ericnicolaas/7cc99114413aee1d0ac8d7ec05e4a1ad to your computer and use it in GitHub Desktop.
Add an email tag for a custom Charitable campaign field
<?php
/**
* In Easy Digital Downloads, you can add your own email tags to include
* in purchase receipts & notifications.
*
* In this example, we add a tag that allows you to display the value of
* a custom 'campaign_email_content' field in Charitable. This is an optional
* follow-up to a tutorial published on our blog.
*
* @see https://www.wpcharitable.com/tutorial-how-to-add-campaign-specific-content-to-the-donation-receipt-email/
*/
/**
* Register the tag.
*
* Note that the first parameter passed to the edd_add_email_tag() function
* will be the name of the tag.
*
* @see http://docs.easydigitaldownloads.com/article/497-edd-add-email-tag
*
* @param int $payment_id
*/
add_action( 'edd_add_email_tags', function( $payment_id ) {
/**
* Add the tag.
*
* This function receives three parameters:
*
* $tag_name The name of the tag that you will use to display it in your email. So in this case, you
* would use this tag in your email like this: {campaign_email_content}
* $tag_description A description of what the tag shows.
* $callback A callback function that is responsible for getting the value of the tag. This receives a
* $payment_id variable as a parameter.
*/
edd_add_email_tag( 'campaign_email_content', 'Display campaign-specific text', function( $payment_id ) {
$output = '';
/**
* Get all campaign donations linked to a particular payment.
*/
$campaign_donations = Charitable_EDD_Payment::get_instance()->get_campaign_donations_through_payment( $payment_id );
if ( ! $campaign_donations ) {
return $output;
}
/**
* Get the campaign that received the donation.
*/
$campaign_id = current( $campaign_donations )->campaign_id;
$campaign = charitable_get_campaign( $campaign_id );
/**
* Set the `value` of our field to the campaign's email content.
*/
$output = $campaign->get( 'campaign_email_content' );
return $output;
} );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment