Skip to content

Instantly share code, notes, and snippets.

@strangerstudios
Last active October 19, 2022 09:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save strangerstudios/048e1e6abd010f2a26cc to your computer and use it in GitHub Desktop.
Save strangerstudios/048e1e6abd010f2a26cc to your computer and use it in GitHub Desktop.
Add "Purchase Additional Access" box to Membership Checkout for a la carte category purchase.
/*
Require specific category user meta to view posts in designated categories.
Custom price-adjusting fields are added via PMPro Register Helper add on (required).
Add all of this code to your active theme's functions.php or a custom plugin.
*/
add_filter("pmpro_has_membership_access_filter", "my_pmpro_additional_categories", 10, 4);
function my_pmpro_additional_categories($hasaccess, $thepost, $theuser, $post_membership_levels)
{
global $wpdb;
//if PMPro says false already, return false
if(!$hasaccess)
return false;
//if the post is in the category and the user has not purchased the category, then block
if(has_category('cooking', $thepost))
{
$cooking = get_user_meta($theuser->ID, "cooking", true);
if(empty($cooking))
$hasaccess = false;
}
if(has_category('baking', $thepost))
{
$baking = get_user_meta($theuser->ID, "baking", true);
if(empty($baking))
$hasaccess = false;
}
if(has_category('crafts', $thepost))
{
$crafts = get_user_meta($theuser->ID, "crafts", true);
if(empty($crafts))
$hasaccess = false;
}
return $hasaccess;
}
//add fields
function my_pmprorh_init()
{
//don't break if Register Helper is not loaded
if(!function_exists("pmprorh_add_registration_field"))
{
return false;
}
$fields[] = new PMProRH_Field(
"cooking", // input name, will also be used as meta key
"checkbox", // type of field
array(
"text"=>"Cooking Category ($10)",
"memberslistcsv"=>true,
"profile"=>"admin_only",
"showmainlabel"=>false,
));
$fields[] = new PMProRH_Field(
"baking", // input name, will also be used as meta key
"checkbox", // type of field
array(
"text"=>"Baking Category ($10)",
"memberslistcsv"=>true,
"profile"=>"admin_only",
"showmainlabel"=>false,
));
$fields[] = new PMProRH_Field(
"crafts", // input name, will also be used as meta key
"checkbox", // type of field
array(
"text"=>"Crafts Category ($10)",
"memberslistcsv"=>true,
"profile"=>"admin_only",
"showmainlabel"=>false,
));
//add the fields into a new checkout_boxes are of the checkout page
pmprorh_add_checkout_box("additional_categories", "Purchase Additional Access");
foreach($fields as $field)
pmprorh_add_registration_field(
"additional_categories", // location on checkout page
$field // PMProRH_Field object
);
//that's it. see the PMPro Register Helper readme for more information and examples.
}
add_action("init", "my_pmprorh_init");
/*
If a user checked options, then adjust the price.
*/
function my_pmpro_checkout_level($level)
{
if(!empty($_REQUEST['cooking']))
{
$level->initial_payment = $level->initial_payment + 10;
$level->billing_amount = $level->billing_amount + 10;
}
if(!empty($_REQUEST['baking']))
{
$level->initial_payment = $level->initial_payment + 10;
$level->billing_amount = $level->billing_amount + 10;
}
if(!empty($_REQUEST['crafts']))
{
$level->initial_payment = $level->initial_payment + 10;
$level->billing_amount = $level->billing_amount + 10;
}
return $level;
}
add_filter("pmpro_checkout_level", "my_pmpro_checkout_level");
@laurenhagan0306
Copy link

This recipe is included in the blog post on "Sell Access to Post Categories “à la carte” Using Price-Adjusting Register Helper Fields" at Paid Memberships Pro here: https://www.paidmembershipspro.com/sell-access-to-helper-fields/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment