Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save andrewlimaza/e293e7e4e593ebdf0f85dc0bfc86f996 to your computer and use it in GitHub Desktop.
Save andrewlimaza/e293e7e4e593ebdf0f85dc0bfc86f996 to your computer and use it in GitHub Desktop.
Change Membership Level Checked Out According to Register Helper Input
<?php
/**
* This will change the user's membership level if the user selects 'lessthan2000' on th custom field.
* Adjust this code to match your needs. Please note that if the initial level checking out is free,
* and you want to assign to a paid level depending on field selected, you may need to adjust the new level's price to $0 or ensure both levels are free or paid.
*
* Add this code to your PMPro Customizations Plugin - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*/
function my_pmprorh_init()
{
//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(
'budget', // input name, will also be used as meta key
'select', // type of field
array(
'options' => array( // <option> elements for select field
'' => '', // blank option - cannot be selected if this field is required
'lessthan2000' => '&lt; $2,000', // <option value="lessthan2000">&lt; $2,000</option>
'2000to5000' => '$2,000-$5,000', // <option value="2000to5000">$2,000-$5,000</option>
'5000to10000' => '$5,000-$10,000', // <option value="5000to10000">$5,000-$10,000</option>
'morethan10000' => '&gt; $10,000', // <option value="morethan10000">&gt; $10,000</option>
)
)
);
//add the fields into a new checkout_boxes are of the checkout page
foreach($fields as $field)
pmprorh_add_registration_field(
'checkout_boxes', // 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' );
function my_pmpro_checkout_level($level)
{
if ( ! empty( $_REQUEST['budget'] ) && $level->id == '4' ) {
if ( $_REQUEST['budget'] == 'lessthan2000' ) {
$level->id = '6';
}
}
return $level;
}
add_filter("pmpro_checkout_level", "my_pmpro_checkout_level");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment