Skip to content

Instantly share code, notes, and snippets.

@greathmaster
Created April 10, 2018 06:41
Show Gist options
  • Save greathmaster/dc06474388e31e07abc3d25344a1a6e3 to your computer and use it in GitHub Desktop.
Save greathmaster/dc06474388e31e07abc3d25344a1a6e3 to your computer and use it in GitHub Desktop.
Allows customers to select membership duration at checkout. Adjusts charge and expiration date accordingly.
/*
Allows customers to select membership duration at checkout. Adjusts charge and expiration date accordingly.
*/
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(
'membership_duration',
'select',
array(
'label' => 'Membership Duration',
'options' => array(1 => '1 Year', 2 => '2 Years', 3 => '3 Years')
)
);
//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)
{
$duration = $_REQUEST['membership_duration'];
if($duration === '1 Year')
$duration = 1;
if($duration === '2 Years')
$duration = 2;
if($duration === '3 Years')
$duration = 3;
if(is_int($duration))
{
$level->expiration_number = $duration;
$level->expiration_period = 'Year';
$level->initial_payment = $duration*$level->initial_payment;
}
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