Created
March 25, 2026 20:47
-
-
Save rickalday/e55851dca8fb9258d80775e47744dd25 to your computer and use it in GitHub Desktop.
Fix donor export for donors with multiple mailing addresses
This file contains hidden or 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 | |
| add_filter('give_export_get_data_donors', function($exportData) { | |
| global $wpdb; | |
| /** | |
| * Map of output column names to their meta key prefixes | |
| */ | |
| $addressFields = [ | |
| 'address_line1' => '_give_donor_address_billing_line1', | |
| 'address_line2' => '_give_donor_address_billing_line2', | |
| 'address_city' => '_give_donor_address_billing_city', | |
| 'address_state' => '_give_donor_address_billing_state', | |
| 'address_zip' => '_give_donor_address_billing_zip', | |
| 'address_country' => '_give_donor_address_billing_country', | |
| ]; | |
| // Get the table prefix | |
| $donorTable = $wpdb->prefix . 'give_donors'; | |
| $donorMetaTable = $wpdb->prefix . 'give_donormeta'; | |
| // Process each row in the export data | |
| foreach ($exportData as &$row) { | |
| // Get the donor ID from the row - try both possible identifiers | |
| $donorId = null; | |
| // First, try to get donor ID from the row data by querying for the email or name | |
| if (isset($row['email']) && !empty($row['email'])) { | |
| $donorId = $wpdb->get_var( | |
| $wpdb->prepare( | |
| "SELECT id FROM {$donorTable} WHERE email = %s LIMIT 1", | |
| $row['email'] | |
| ) | |
| ); | |
| } | |
| // If still no donor ID, try by name | |
| if (!$donorId && isset($row['full_name']) && !empty($row['full_name'])) { | |
| $donorId = $wpdb->get_var( | |
| $wpdb->prepare( | |
| "SELECT id FROM {$donorTable} WHERE name = %s LIMIT 1", | |
| $row['full_name'] | |
| ) | |
| ); | |
| } | |
| // If we still don't have a donor ID, skip this row | |
| if (!$donorId) { | |
| continue; | |
| } | |
| // For each address field, check if we need to look for alternate suffixes | |
| foreach ($addressFields as $outputKey => $fieldBase) { | |
| // If the field is null or empty, try _0 through _10 | |
| if (!isset($row[$outputKey]) || is_null($row[$outputKey]) || $row[$outputKey] === '') { | |
| $row[$outputKey] = ''; // Initialize with empty string | |
| for ($i = 0; $i <= 10; $i++) { | |
| $metaKey = $fieldBase . '_' . $i; | |
| $value = $wpdb->get_var( | |
| $wpdb->prepare( | |
| "SELECT meta_value FROM {$donorMetaTable} | |
| WHERE donor_id = %d AND meta_key = %s LIMIT 1", | |
| $donorId, | |
| $metaKey | |
| ) | |
| ); | |
| if (!empty($value)) { | |
| $row[$outputKey] = $value; | |
| break; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| return $exportData; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment