Forked from travislima/pmpro-select-membership-duration.php
Last active
February 19, 2019 17:09
-
-
Save andrewlimaza/b4df909e3d8738a594985b259855ffb1 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.
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 | |
/* | |
* Add this code below to your PMPro Customizations Plugin - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
* Allows customers to select membership duration at checkout. Adjusts the amount charged and expiration date of the membership accordingly. | |
* 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/ | |
*/ | |
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' => '--', 2 => '2 Years', 3 => '3 Years' ), | |
'levels' => array( 2 ) // Make sure this is to only show for yearly membership options. | |
) | |
); | |
//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' ); | |
// Adds the duration functionality that alters the amount charged and the expiration date of the membership depending on what the user selects. | |
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" ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment