Created
May 21, 2024 21:23
-
-
Save DevinWalker/0dd6c3586f464e3f9fcd1e067aac778f to your computer and use it in GitHub Desktop.
GiveWP Import Fee Recovery Fields
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
function my_custom_give_import_donation_fields( $options ) { | |
// Add 'Donor Age' field | |
$options['_give_fee_amount'] = [ | |
__( 'Recovered Fee', 'give' ), | |
__( 'Fee Recovery', 'give' ), | |
__( 'Fee Recovered', 'give' ), | |
__( 'Fee Covered', 'give' ), | |
]; | |
// Is this a subscription? | |
$options['_give_subscription_payment'] = [ | |
__( 'Subscription', 'give' ), | |
__( 'Recurring Donation', 'give' ) | |
]; | |
// Stripe: | |
$options['_give_payment_transaction_id'] = [ | |
__( 'Transaction ID', 'give' ), | |
]; | |
// Comment: | |
$options['_give_donation_comment'] = [ | |
__( 'Comment', 'give' ), | |
__( 'Message', 'give' ), | |
__( 'Donor Message', 'give' ) | |
]; | |
// Anonymous | |
$options['_give_anonymous_donor'] = [ | |
__( 'Anonymous Donation', 'give' ), | |
__( 'Show name', 'give' ), | |
__( 'Anonymous', 'give' ), | |
]; | |
// Return the modified options | |
return $options; | |
} | |
add_filter( 'give_import_donations_options', 'my_custom_give_import_donation_fields' ); | |
function devin_custom_give_import_donor_fields( $options ) { | |
// Add 'Donor Age' field | |
$options['phone_number'] = [ | |
__( 'Phone Number', 'give' ), | |
__( 'Donor Phone Number', 'give' ), | |
__( 'Donor Phone', 'give' ), | |
__( 'Donor Number', 'give' ), | |
]; | |
// Return the modified options | |
return $options; | |
} | |
add_filter( 'give_import_donor_options', 'devin_custom_give_import_donor_fields' ); | |
/** | |
* Updates the payment data with custom fields from the raw data. | |
* | |
* @param int $payment_id payment id | |
* @param array $payment_data donation data | |
* @param $data | |
* @param object $donor_data form object | |
* @param $form | |
* | |
* @return void | |
*/ | |
function my_custom_give_import_payment_handler( $payment_id, $payment_data, $data, $donor_data, $form ): void { | |
$payment = new Give_Payment( $payment_id ); | |
$donor = new Give_Donor( $payment->donor_id ); | |
// Check if the custom field '_give_donation_comment' is set in the raw data ($data array) | |
if ( isset( $data['_give_donation_comment'] ) ) { | |
$payment->update_meta( '_give_donation_comment', sanitize_text_field( $data['_give_donation_comment'] ) ); | |
} | |
// Add Fee Recovery import data support. | |
if ( isset( $data['_give_fee_amount'] ) && function_exists('give_fee_number_format') ) { | |
// Fee recovery requires two meta fields to be updated in order to calculate totals properly. | |
$payment->update_meta( '_give_fee_donation_amount', $payment->total ); | |
$payment->update_meta( '_give_fee_amount', give_fee_number_format( $data['_give_fee_amount'], true, $payment_id ) ); | |
} | |
// Stripe ID | |
if ( isset( $data['_give_payment_transaction_id'] ) ) { | |
$payment->update_meta( '_give_payment_transaction_id', $data['_give_payment_transaction_id'] ); | |
} | |
// Anonymous Donation? | |
if ( isset( $data['_give_anonymous_donor'] ) && 'No' === $data['_give_anonymous_donor'] ) { | |
$payment->update_meta( '_give_anonymous_donation', 1 ); | |
$donor->update_meta( '_give_anonymous_donor', 1 ); | |
} | |
} | |
add_action( 'give_import_after_import_payment', 'my_custom_give_import_payment_handler', 10, 5 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@DevinWalker I think I found the issue with this import:
On line 83:
Needs to be the total minus fees. This meta is supposed to be the "intended amount" donated. Pseudo example below: