Skip to content

Instantly share code, notes, and snippets.

@strangerstudios
Created March 22, 2016 18:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save strangerstudios/9795acefd7606e4589f2 to your computer and use it in GitHub Desktop.
Save strangerstudios/9795acefd7606e4589f2 to your computer and use it in GitHub Desktop.
Charge different prices for different membership levels with PMPro and PMPro Addon Packages.
/*
Charge different prices for different membership levels.
*/
//global var to store the price configurations
global $custom_addon_package_prices;
$custom_addon_package_prices = array(
//post_id => array(level_id => price, level_id => price, ...)
353 => array(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;
//only affects logged in members
if(!is_user_logged_in())
return $level;
//are we purchasing a post?
if(!empty($_REQUEST['ap'])) {
$ap = intval($_REQUEST['ap']);
$ap_post = get_post($ap);
global $custom_addon_package_prices;
if(isset($custom_addon_package_prices[$ap]) && isset($custom_addon_package_prices[$ap][$level->id])) {
//updates initial_payment and billing amounts if they are non-zero
if(!empty($level->initial_payment))
$level->initial_payment = $custom_addon_package_prices[$ap][$level->id];
if(!empty($level->billing_amount))
$level->billing_amount = $custom_addon_package_prices[$ap][$level->id];
}
}
return $level;
}
add_filter("pmpro_checkout_level", "custom_addon_package_prices", 20);
//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[$post->ID]) && !empty($custom_addon_package_prices[$post->ID][$text_level_id]))
$pmproap_price = $custom_addon_package_prices[$post->ID][$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