Last active
March 10, 2025 13:23
-
-
Save davidmutero/4a882fff3389021de4afe83e6cabb669 to your computer and use it in GitHub Desktop.
Modify checkout message on restricted donation level for members switching
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 | |
/** | |
* Modify the checkout message based on the "Donations-Only Level" setting. | |
* | |
* Ensures that existing members are only prevented from checking out with the donation-only level | |
* if the "Donations-Only Level" option is checked in the membership level settings. | |
*/ | |
/** | |
* | |
* @param string $content The original checkout message. | |
* @return string The modified message if conditions are met. | |
*/ | |
function my_pmpro_modify_donation_checkout_message( $content ) { | |
global $pmpro_level, $current_user; | |
// Ensure PMPro functions exist and the level is set. | |
if ( ! function_exists( 'pmprodon_is_donations_only' ) || empty( $pmpro_level ) || empty( $pmpro_level->id ) ) { | |
return $content; | |
} | |
// Ensure we are checking out for a donations-only level. | |
if ( ! pmprodon_is_donations_only( $pmpro_level->id ) ) { | |
return $content; // Exit if this level is not donations-only. | |
} | |
// Ensure user already has an active membership before modifying the message. | |
if ( ! empty( $current_user->membership_level ) ) { | |
// Get the current membership level name. | |
$current_level_name = '<strong>' . esc_html( $current_user->membership_level->name ) . '</strong>'; | |
// Modify the checkout message. | |
$content = preg_replace( | |
'/Your current membership level of .*? will be removed when you complete your purchase\./', | |
'You cannot checkout with this donation level because you already have the ' . $current_level_name . ' membership level.', | |
$content | |
); | |
} | |
return $content; | |
} | |
add_filter( 'the_content', 'my_pmpro_modify_donation_checkout_message', 20 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment