Last active
March 27, 2021 00:14
-
-
Save ideadude/fe072277b66132639569e36ceb767162 to your computer and use it in GitHub Desktop.
Assign additional membership levels based on fields at checkout.
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 | |
/** | |
* 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 ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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/