Skip to content

Instantly share code, notes, and snippets.

@ideadude
Created November 21, 2019 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ideadude/b2051ad4db5900b39a14ca62cd387ff1 to your computer and use it in GitHub Desktop.
Save ideadude/b2051ad4db5900b39a14ca62cd387ff1 to your computer and use it in GitHub Desktop.
Donation notes field example for PMPro Donations.
<?php
/**
* Donation notes.
* Edit the copy below.
* Add to a custom plugin.
* Notes are saved into the notes field of the order.
*/
// show the notes field at checkout.
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
if ( ! empty( $_REQUEST['pmpro_donation_notes'] ) ) {
$pmpro_donation_notes = sanitize_text_field( $_REQUEST['pmpro_donation_notes'] );
} else {
$pmpro_donation_notes = '';
}
// show field and instructions
?>
<p class="pmpro_small donation_notes_trigger_p">
All donations are designated for the general fund. <a href="#" class="donation_notes_trigger">To specify how your donation is used, click here.</a></p>
<p class="donation_notes_instructions" style="display: none;">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="display: none;"><?php echo esc_textarea( $pmpro_donation_notes ); ?></textarea>
<script>
jQuery(document).ready(function() {
function show_pmpro_donation_notes() {
jQuery('#pmpro_donation_notes').show();
jQuery('p.donation_notes_trigger_p').hide();
jQuery('p.donation_notes_instructions').show();
}
jQuery('a.donation_notes_trigger').click(function() {
show_pmpro_donation_notes();
});
if ( jQuery('#pmpro_donation_notes').val().length > 0 ) {
show_pmpro_donation_notes();
}
});
</script>
<?php
}
add_action( 'pmpro_checkout_after_level_cost', 'my_pmprodon_donation_notes', 15 );
// save donation notes into order notes
function my_pmprodon_save_donation_notes_to_order( $order ) {
if ( !empty( $_REQUEST['pmpro_donation_notes'] ) ) {
$value = sanitize_text_field( $_REQUEST['pmpro_donation_notes'] );
} elseif ( ! empty( $_SESSION['pmpro_donation_notes'] ) ) {
$value = sanitize_text_field( $_SESSION['pmpro_donation_notes'] );
} else {
$value = false;
}
if ( $value ) {
if( !isset( $order->notes ) ) {
$order->notes = 'Donation Notes: ' . $value . "\n";
} else {
$order->notes .= "\nDonation Notes: " . $value . "\n";
}
}
return $order;
}
add_filter( 'pmpro_checkout_order', 'my_pmprodon_save_donation_notes_to_order' );
// save notes in session
function my_pmprodon_save_donation_notes_to_session() {
if ( !empty( $_REQUEST['pmpro_donation_notes'] ) ) {
$_SESSION['pmpro_donation_notes'] = $_REQUEST['pmpro_donation_notes'];
}
}
add_action( 'pmpro_checkout_before_processing', 'my_pmprodon_save_donation_notes_to_session' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment