Last active
October 16, 2024 15:19
-
-
Save ideadude/1ab18a3a22ba52e8b2a4d7f631025d1a to your computer and use it in GitHub Desktop.
Override the default is_downgrade check in the PMPro Proration Add On
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
/** | |
* Override the default is_downgrade check in the PMPro Proration Add On | |
* Add this code to a custom plugin. | |
* A rank of level orders is used to calculate downgrades from upgrades. | |
* Be sure to change the $level_order array below as needed. | |
* You can also do your own calculation on the $old_level and $new_level objects. | |
*/ | |
function pmpro_is_downgrade_custom_filter( $is_downgrade, $old_level, $new_level ) { | |
// this array contains all of the level ids in order of downgrade -> upgrade | |
// change this as needed | |
$level_order = array(4, 5, 6, 1, 2, 3); | |
// figure out where the levels rank | |
$old_level_rank = array_search( $old_level, $level_order ); | |
$new_level_rank = array_search( $new_level, $level_order ); | |
// make sure we have a rank for both levels, otherwise skip to let the plugin guess | |
if( $old_level_rank !== false && $new_level_rank !== false ) { | |
if( $old_level_rank > $new_level_rank ) { | |
$is_downgrade = true; | |
} else { | |
$is_downgrade = false; | |
} | |
} | |
return $is_downgrade; | |
} | |
add_filter( 'pmpro_is_downgrade', 'pmpro_is_downgrade_custom_filter', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated 16OCT24 because the filter is now passing in level ids instead of level objects.
See: https://github.com/strangerstudios/pmpro-proration/blob/e2834c41cb793a455ef9b34636e394d2ab2ae97c/includes/checkout.php#L66