Last active
August 2, 2024 19:13
-
-
Save MaximilianoRicoTabo/67a672fdd9923bcd59aa53078c4678ff to your computer and use it in GitHub Desktop.
Customize the level cost text to include the donation amount.
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 | |
/** | |
* Customize the level cost text to include the donation amount. | |
* link: TBD | |
* | |
* You can add this recipe to your site by creating a custom plugin | |
* or using the Code Snippets plugin available for free in the WordPress repository. | |
* Read this companion article for step-by-step directions on either method. | |
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations | |
*/ | |
/** | |
* Customize the level cost text to include the donation amount. | |
*/ | |
function customize_level_cost_text() { | |
?> | |
<script type="text/javascript"> | |
//Add dom ready function to watch donation amount for changes and update level cost text total. | |
jQuery('document').ready(function($) { | |
$( '#donation_dropdown' ).on( 'change', function() { | |
if( $(this).val() != 'other' ) { | |
//Get the selected donation amount | |
const donation_amount = $(this).val(); | |
updateTotal( donation_amount ); | |
} | |
}); | |
$( '#donation' ).on( 'change', function() { | |
//Get the selected donation amount | |
const donation_amount = $(this).val(); | |
updateTotal( donation_amount ); | |
}); | |
function updateTotal( donation_amount ) { | |
const level_cost = $( '#pmpro_level_cost p' ).text(); | |
$('.custom-donation').empty(); | |
$( '#pmpro_level_cost p strong' ).after( $('<span/>') | |
.addClass('custom-donation').append('( plus $ ' + donation_amount + ' ) as a donation' ) ); | |
} | |
}); | |
</script> | |
<?php | |
} | |
add_action( 'pmpro_checkout_after_form', 'customize_level_cost_text' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment