Skip to content

Instantly share code, notes, and snippets.

@ericnicolaas
Created March 13, 2020 01:16
Show Gist options
  • Save ericnicolaas/bed522f31f072d4a8e114a5bfb14dd52 to your computer and use it in GitHub Desktop.
Save ericnicolaas/bed522f31f072d4a8e114a5bfb14dd52 to your computer and use it in GitHub Desktop.
Adding new field to donations export based on custom campaign field
<?php
/**
* In this example, we add a custom field to campaigns, which is
* automatically included in the Donations export for donations
* to those campaigns.
*/
add_action(
'init',
function() {
/**
* Step 1: Register the campaign field.
*
* This will add a new field when you are editing a
* campaign allowing you to define the Fund Code for
* that campaign.
*/
$campaign_field = new Charitable_Campaign_Field(
'Fund_Code',
array(
'label' => 'Fund Code',
'data_type' => 'meta',
'admin_form' => array(
'type' => 'text',
'required' => false,
),
'show_in_export' => true,
'value_callback' => false,
)
);
charitable()->campaign_fields()->register_field( $campaign_field );
/**
* Step 2: Register the donation field.
*
* This will add it to the donations export. For each
* donation, it will look up the "Fund Code" for the campaign
* that received the donation, and add that in its own column.
*/
$donation_field = new Charitable_Donation_Field(
'Fund_Code',
array(
'label' => 'Fund Code',
'data_type' => 'core',
'donation_form' => false,
'admin_form' => false,
'show_in_export' => true,
'value_callback' => function( Charitable_Donation $donation ) {
$campaign_id = current( $donation->get_campaign_donations() )->campaign_id;
return charitable_get_campaign( $campaign_id )->get( 'Fund_Code' );
},
)
);
charitable()->donation_fields()->register_field( $donation_field );
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment