Forked from strangerstudios/custom_addon_package_prices.php
Last active
June 6, 2023 15:41
-
-
Save kimwhite/b4683328eceefd754db7ab6408f7a2e1 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 // do not copy this line. | |
/** | |
* This recipe Adds different prices for different membership levels. | |
* For use with with Addon Package Add On https://www.paidmembershipspro.com/add-ons/pmpro-purchase-access-to-a-single-page/ | |
* | |
* 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/ | |
*/ | |
//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), | |
355 => array(1 => 800, 2 => 600, 3 => 400), // copy this line for other post id's | |
); | |
//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