Forked from kimwhite/pmpro-one-time-signup-fee.php
Last active
September 25, 2020 13:59
-
-
Save ipokkel/dd0fbdafb50dd69e6134f7c64d4001f7 to your computer and use it in GitHub Desktop.
This recipe will allow you to charge additional extras on the user's fee. #pmpro #checkout
This file contains 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 | |
/** | |
* This recipe will allow you to charge additional extras on the user's fee. | |
* | |
* You can add this recipe to your site by creating a custom plugin | |
* or using the Code Snippets plugin available for free in the WordPress repository. | |
* Read this companion article for step-by-step directions on either method. | |
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
*/ | |
//we have to put everything in a function called on init, so we are sure Register Helper is loaded | |
function my_pmprorh_init_signup_fee() { | |
//don't break if Register Helper is not loaded | |
if ( ! function_exists( 'pmprorh_add_registration_field' ) ) { | |
return false; | |
} | |
$fields = array(); | |
//define the fields | |
global $current_user; | |
$user_id = $current_user->ID; | |
$signupfee = get_user_meta( $user_id, 'signupfee', true ); | |
/* | |
only request signup fee for new level 1 members | |
OR | |
any user who has previously paid a signup fee. | |
*/ | |
if ( ( function_exists( 'pmpro_hasMembershipLevel' ) && ! pmpro_hasMembershipLevel( 1 ) ) || 1 !== intval( $signupfee ) ) { | |
$fields[] = new PMProRH_Field( | |
'signupfee', // input name, will also be used as meta key | |
'checkbox', // type of field | |
array( | |
'label' => 'Signup Fee $30', // custom field label | |
'hint' => 'I understand there is a new members fee', | |
'levels' => 1, | |
'required' => true, | |
) | |
); | |
} | |
//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_signup_fee' ); | |
function my_pmpro_checkout_level_signupfee( $level ) { | |
if ( ! empty( $_REQUEST['signupfee'] ) ) { | |
$level->initial_payment = $level->initial_payment + 30; //to update the initial payment. | |
// $level->billing_amount = $level->billing_amount + 175; //to update recurring payments too | |
} | |
return $level; | |
} | |
add_filter( 'pmpro_checkout_level', 'my_pmpro_checkout_level_signupfee' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment