Forked from strangerstudios/custom_addon_package_prices.php
Last active
January 12, 2021 15:57
-
-
Save dparker1005/6f1e5f5908689ccb6bc2a055f44e352e to your computer and use it in GitHub Desktop.
Charge different prices for different membership levels with PMPro and PMPro Addon Packages.
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 | |
// Copy from below here... | |
/* | |
* Charge different prices for different membership levels. | |
*/ | |
//global var to store the price configurations | |
global $custom_addon_package_prices; | |
$custom_addon_package_prices = array( | |
//level_id => price | |
1 => 1500, | |
2 => 1000, | |
3 => 500, | |
); | |
//function to addjust the price | |
function custom_addon_package_prices($level) { | |
//make sure addon packages is activated | |
if(!function_exists('pmproap_addMemberToPost')) | |
return $level; | |
//are we purchasing a post? | |
if(!empty($_REQUEST['ap'])) { | |
$ap = intval($_REQUEST['ap']); | |
global $custom_addon_package_prices; | |
if(isset($custom_addon_package_prices[$level->id])) { | |
// Remove the original PMProAP filter... | |
remove_filter( 'pmpro_checkout_level', 'pmproap_pmpro_checkout_level' ); | |
//updates initial_payment and billing amounts if they are non-zero | |
$level->initial_payment += $custom_addon_package_prices[$level->id]; | |
} | |
} | |
return $level; | |
} | |
add_filter("pmpro_checkout_level", "custom_addon_package_prices", 9); | |
//adjust the price in non-member/etc text | |
function custom_addon_package_prices_text_filter($text) { | |
global $wpdb, $current_user, $post, $pmpro_currency_symbol; | |
//make sure pmpro addon packages is active | |
if(!function_exists('pmproap_hasAccess')) | |
return $text; | |
//alterred version of filter that comes in pmpro addon packages | |
if(!empty($post)) | |
{ | |
if(pmproap_isPostLocked($post->ID) && !pmproap_hasAccess($current_user->ID, $post->ID)) | |
{ | |
//which level to use for checkout link? | |
$text_level_id = pmproap_getLevelIDForCheckoutLink($post->ID, $current_user->ID); | |
//what's the price | |
global $custom_addon_package_prices; | |
if(!empty($custom_addon_package_prices) && !empty($custom_addon_package_prices[$text_level_id])) | |
$pmproap_price = $custom_addon_package_prices[$text_level_id]; | |
else | |
$pmproap_price = get_post_meta($post->ID, "_pmproap_price", true); | |
//check for all access levels | |
$all_access_levels = apply_filters("pmproap_all_access_levels", array(), $current_user->ID, $post->ID); | |
//update text | |
if(!empty($all_access_levels)) | |
{ | |
$level_names = array(); | |
foreach ($all_access_levels as $level_id) | |
{ | |
$level = pmpro_getLevel($level_id); | |
$level_names[] = $level->name; | |
} | |
$text = "<p>This content requires that you purchase additional access. The price is " . $pmpro_currency_symbol . $pmproap_price . " or free for our " . pmpro_implodeToEnglish($level_names) . " members.</p>"; | |
$text .= "<p><a href=\"" . pmpro_url("checkout", "?level=" . $text_level_id . "&ap=" . $post->ID) . "\">Purchase this Content (" . pmpro_formatPrice($pmproap_price) . ")</a> <a href=\"" . pmpro_url("levels") . "\">Choose a Membership Level</a></p>"; | |
} | |
else | |
{ | |
$text = "<p>This content requires that you purchase additional access. The price is " . pmpro_formatPrice($pmproap_price) . ".</p>"; | |
$text .= "<p><a href=\"" . pmpro_url("checkout", "?level=" . $text_level_id . "&ap=" . $post->ID) . "\">Click here to checkout</a></p>"; | |
} | |
} | |
} | |
return $text; | |
} | |
add_filter("pmpro_non_member_text_filter", "custom_addon_package_prices_text_filter", 20); | |
add_filter("pmpro_not_logged_in_text_filter", "custom_addon_package_prices_text_filter", 20); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment