Forked from davidmutero/my_pmprodon_donation_notes.php
Last active
February 14, 2025 15:28
-
-
Save kimwhite/f8226a144fa4f8360863f142f3118cb1 to your computer and use it in GitHub Desktop.
Donation notes field example for PMPro Donations (Updated).
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 | |
/** | |
* Donation notes. | |
* Edit the copy below. | |
* Add to a custom plugin. | |
* Notes are now saved into the order meta instead of the order notes. | |
*/ | |
// Show the donation notes field at checkout (Always visible) | |
function my_pmprodon_donation_notes() { | |
global $pmpro_level; | |
// Check if this level has donations | |
$donfields = get_option('pmprodon_' . $pmpro_level->id); | |
// No donations? Just return | |
if (empty($donfields) || empty($donfields['donations'])) { | |
return; | |
} | |
// Grab data | |
$pmpro_donation_notes = isset($_REQUEST['pmpro_donation_notes']) ? sanitize_text_field($_REQUEST['pmpro_donation_notes']) : ''; | |
// Show field and instructions (always visible) | |
?> | |
<p class="pmpro_small">All donations are designated for the general fund.</p> | |
<p class="donation_notes_instructions"> | |
Your donation can be earmarked for the general fund, the scholarship fund, or a combination of both. You can also request a specific use for your donation. Let us know in the field below. | |
</p> | |
<textarea id="pmpro_donation_notes" name="pmpro_donation_notes" style="width: 100%; height: 100px;"><?php echo esc_textarea($pmpro_donation_notes); ?></textarea> | |
<?php | |
} | |
add_action('pmpro_checkout_after_level_cost', 'my_pmprodon_donation_notes', 15); | |
// Save donation notes into order meta | |
function my_pmprodon_save_donation_notes_to_order($order) { | |
if (!empty($_REQUEST['pmpro_donation_notes'])) { | |
$donation_notes = sanitize_text_field($_REQUEST['pmpro_donation_notes']); | |
update_post_meta($order->id, 'pmpro_donation_notes', $donation_notes); | |
} | |
} | |
add_action('pmpro_added_order', 'my_pmprodon_save_donation_notes_to_order'); | |
// Retrieve donation notes when viewing order details | |
function my_pmprodon_show_donation_notes($order) { | |
$donation_notes = get_post_meta($order->id, 'pmpro_donation_notes', true); | |
if (!empty($donation_notes)) { | |
echo '<p><strong>Donation Notes:</strong> ' . esc_html($donation_notes) . '</p>'; | |
} | |
} | |
add_action('pmpro_after_order_settings', 'my_pmprodon_show_donation_notes'); | |
// Save donation notes in session to persist on checkout reloads | |
function my_pmprodon_save_donation_notes_to_session() { | |
if (!empty($_REQUEST['pmpro_donation_notes'])) { | |
$_SESSION['pmpro_donation_notes'] = sanitize_text_field($_REQUEST['pmpro_donation_notes']); | |
} | |
} | |
add_action('pmpro_checkout_before_processing', 'my_pmprodon_save_donation_notes_to_session'); | |
// Retrieve saved donation notes from session when returning to checkout | |
function my_pmprodon_retrieve_donation_notes_from_session() { | |
if (!empty($_SESSION['pmpro_donation_notes'])) { | |
$_REQUEST['pmpro_donation_notes'] = $_SESSION['pmpro_donation_notes']; | |
} | |
} | |
add_action('pmpro_checkout_after_form', 'my_pmprodon_retrieve_donation_notes_from_session'); | |
// Add the note to the user invoice. | |
function add_custom_postmeta_to_pmpro_invoice( $invoice ) { | |
// Ensure invoice object exists | |
if ( empty( $invoice ) || empty( $invoice->id ) ) { | |
return; | |
} | |
// Get the post meta value using the invoice ID as the post_id | |
$donation_notes = get_post_meta( $invoice->id, 'pmpro_donation_notes', true ); | |
// Only display if there's a value | |
if ( ! empty( $donation_notes ) ) { | |
echo '<li><strong>Donation Notes:</strong> ' . esc_html( $donation_notes ) . '</li>'; | |
} | |
} | |
add_action( 'pmpro_invoice_bullets_bottom', 'add_custom_postmeta_to_pmpro_invoice' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment