Skip to content

Instantly share code, notes, and snippets.

@ideadude
Last active March 27, 2021 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ideadude/fe072277b66132639569e36ceb767162 to your computer and use it in GitHub Desktop.
Save ideadude/fe072277b66132639569e36ceb767162 to your computer and use it in GitHub Desktop.
Assign additional membership levels based on fields at checkout.
<?php
/**
* Assign additional membership levels based on fields at checkout.
* Requires PMPro, PMPro MMPU, and PMPro Register Helper.
* This is just an example. Be sure to change the field settings,
* field meta name, and membership levels to fit your needs.
*/
// Add a checkbox via Register Helper
function my_amlbofat_add_fields() {
// Don't break if Register Helper is not loaded.
if ( ! function_exists( 'pmprorh_add_registration_field' ) ) {
return false;
}
// Define the fields.
$fields = array();
$fields[] = new PMProRH_Field(
'include_level_2',
'checkbox',
array(
'label' => 'Include Level 2 Content?',
'save_function' => 'my_amlbofat_save_function', // !! NOTE !!
'profile' => true,
)
);
// Add the fields into a new checkout_boxes are of the checkout page.
foreach ( $fields as $field ) {
pmprorh_add_registration_field( 'checkout_boxes', $field );
}
}
add_action( 'init', 'my_amlbofat_add_fields' );
// Give user's an extra level based on a Register Helper field.
function my_amlbofat_save_function( $user_id, $field_name, $value ) {
// Make sure MMPU is active.
if ( ! defined( 'PMPROMMPU_VER' ) ) {
return;
}
// Check field and give user level if appropriate.
if ( $field_name == 'include_level_2' ) {
if ( $value == 1 ) {
pmprommpu_addMembershipLevel( 2, $user_id );
update_user_meta( $user_id, 'include_level_2', 1 );
} else {
pmpro_cancelMembershipLevel( 2, $user_id );
update_user_meta( $user_id, 'include_level_2', 0 );
}
}
}
@laurenhagan0306
Copy link

This recipe is included in the blog post on "Assign Additional Membership Levels Based on Fields at Checkout" at Paid Memberships Pro here: https://www.paidmembershipspro.com/assign-additional-membership-levels-based-on-fields-at-checkout/

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