Created
January 21, 2025 22:10
-
-
Save rickalday/1f8c2c6d876715f06c6ffa6d5e2c3c78 to your computer and use it in GitHub Desktop.
Include custom field data from donor metadata in GiveWP's donation history export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This function sets the column header in the generated CSV file for the custom field. Add an if statement for each aditional field. | |
*/ | |
function givewp_custom_columns_name( $cols ) { | |
if ( isset( $cols['givewp_employer'] ) ) { | |
$cols['givewp_employer'] = __( 'Employer', 'give' ); | |
} | |
return $cols; | |
} | |
add_filter( 'give_export_donation_get_columns_name', 'givewp_custom_columns_name' ); | |
/** | |
* Thi function adds a checkbox to the Donor fields section in the Export page. Add an if statement for each aditional field. | |
*/ | |
function givewp_export_custom_field() { | |
?> | |
<li> | |
<label for="give-export-employer"> | |
<input type="checkbox" checked | |
name="give_give_donations_export_option[givewp_employer]" | |
id="give-export-employer"><?php _e( 'Employer', 'give' ); ?> | |
</label> | |
</li> | |
<?php | |
} | |
add_action( 'give_export_donation_standard_donor_fields', 'givewp_export_custom_field' ); | |
/** | |
* This function populates the CSV with the custom field data. Include additional items in the $data array as needed. | |
*/ | |
use Give\Donors\Models\Donor; | |
function givewp_export_custom_field_data( $data, $payment, $columns, $instance ) { | |
// We use the donor email to get the donor id. | |
$email = $payment->get_meta( '_give_payment_donor_email' ); | |
$donorId = Donor::whereEmail($email)->id; | |
// With the donor id, we can query any custom field stored in the donormeta table | |
$employer = give()->donor_meta->get_meta( $donorId, 'employer' , true); | |
if ( ! empty( $columns['givewp_employer'] )) { | |
if ( $employer ) { | |
$data['givewp_employer'] = $employer ; | |
} else { | |
$data['givewp_employer'] = ''; | |
} | |
} | |
return $data; | |
} | |
add_filter( 'give_export_donation_data', 'givewp_export_custom_field_data', 20, 4 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment