Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save travislima/61b10205028bb3bfb3a075a44d48c17f to your computer and use it in GitHub Desktop.
Save travislima/61b10205028bb3bfb3a075a44d48c17f to your computer and use it in GitHub Desktop.
Allows customers to select membership duration at checkout. Adjusts the amount charged and expiration date of the membership accordingly.
<?php
/**
* Allow customers to select membership duration at checkout. Adjust the amount charged and expiration date of the membership accordingly.
*
* Add this code below to your PMPro Customizations Plugin - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*
* Requires the Paid Memberships Pro Register Helper Add On to be installed and activated in order to work:
* https://www.paidmembershipspro.com/add-ons/pmpro-register-helper-add-checkout-and-profile-fields/
*/
/**
* Instantiate the Register Helper class
*
* @return [type] [description]
*/
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(
'no' => '1 Year',
2 => '2 Years',
3 => '3 Years',
),
'levels' => array( 8 ), // Make sure this is to only show for yearly membership options. Set array number to relevant level ID.
)
);
// 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_pmprorh_init' );
/**
* my_pmpro_checkout_level Alters the amount charged and the expiration date of the membership depending on duration selected by user.
*
* @param object $level The $pmpro_level object prior to duration being added.
*
* @return object $level The callback function returns the modified $pmpro_level object with the added duration.
*/
function my_pmpro_checkout_level( $level ) {
$duration = isset( $_REQUEST['membership_duration'] ) ? $_REQUEST['membership_duration'] : '';
// Bail if no duration is set.
if ( empty( $duration ) ) {
return $level;
}
// Bail if the user hasn't selected a duration and keep level intact.
if ( 'no' === $duration ) {
return $level;
}
// Duration for 2 years, give 20% discount.
if ( '2' === $duration ) {
$duration = 2;
$discount_percentage = 0.2;
}
// Duration for 3 years, give 25% discount.
if ( '3' === $duration ) {
$duration = 3;
$discount_percentage = 0.25;
}
/**
* Set the duration to selected and adjust the charged amount.
*
* Please Note - the new amount will only show upon redirecting to payment gateway page.
*/
if ( is_int( $duration ) ) {
// Set the level's duration.
$level->expiration_number = $duration;
$level->expiration_period = 'Year';
// Let's calculate the new price with discount.
$new_amount = $duration * $level->initial_payment;
$discount_amount = $new_amount * $discount_percentage;
// Set the level's new price.
$level->initial_payment = round( $new_amount - $discount_amount );
}
return $level;
}
add_filter( 'pmpro_checkout_level', 'my_pmpro_checkout_level' );
@laurenhagan0306
Copy link

This recipe is included in the blog post on "Add a Membership Duration dropdown to Checkout and Offer a Discount for Extending Membership" at Paid Memberships Pro here: https://www.paidmembershipspro.com/add-a-membership-duration-dropdown-to-checkout-and-offer-a-discount-for-extending-membership/

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