Skip to content

Instantly share code, notes, and snippets.

@andrewlimaza
Created June 13, 2017 08:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewlimaza/a8ff764e9caa4f1c5ee94b06213e2844 to your computer and use it in GitHub Desktop.
Save andrewlimaza/a8ff764e9caa4f1c5ee94b06213e2844 to your computer and use it in GitHub Desktop.
Register Helper Example With Custom Checkout Boxes
<?php
/**
* Add the following code to your PMPro Customizations Plugin - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
* This function below creates 3 custom checkout boxes and adds fields to these areas respectively using arrays.
*/
function checkout_boxes_example()
{
//don't break if Register Helper is not loaded
if(!function_exists( 'pmprorh_add_registration_field' )) {
return false;
}
//create all checkout boxes here first.
pmprorh_add_checkout_box("personal", "Personal Information");
pmprorh_add_checkout_box("other", "Other Information");
pmprorh_add_checkout_box("third", "Third Information");
//create array for all fields that belong to personal checkout box
$fields_for_personal = array();
$fields_for_personal[] = new PMProRH_Field(
'company', // input name, will also be used as meta key
'text', // type of field
array(
'label' => 'Company' , // custom field label
'class' => 'company', // custom class
'profile' => true, // show in user profile
'required' => true, // make this field required
'levels' => array(1,2) // only levels 1 and 2 should have the company field
)
);
$fields_for_personal[] = new PMProRH_Field(
'my_first_name', // input name, will also be used as meta key
'text', // type of field
array(
'label' => 'First Name' , // custom field label
'class' => 'first_name', // custom class
'profile' => true, // show in user profile
'required' => true, // make this field required
)
);
foreach($fields_for_personal as $field)
pmprorh_add_registration_field(
'personal', // location on checkout page
$field // PMProRH_Field object
);
//fields for personal checkout box end here.
$fields_for_other = array();
$fields_for_other[] = new PMProRH_Field(
'some_field_meta', // input name, will also be used as meta key
'text', // type of field
array(
'label' => 'Some Field' , // custom field label
'class' => 'some_field', // custom class
'profile' => true, // show in user profile
'required' => true, // make this field required
'levels' => array(1,2)
)
);
$fields_for_other[] = new PMProRH_Field(
'another_field', // input name, will also be used as meta key
'text', // type of field
array(
'label' => 'Another Field' , // custom field label
'class' => 'another_field', // custom class
'profile' => true, // show in user profile
'required' => true, // make this field required
)
);
foreach($fields_for_other as $field)
pmprorh_add_registration_field(
'other', // location on checkout page
$field // PMProRH_Field object
);
//fields for Other checkout box end here
$fields_for_third = array();
$fields_for_third[] = new PMProRH_Field(
'a_field', // input name, will also be used as meta key
'text', // type of field
array(
'label' => 'A Field' , // custom field label
'class' => 'a_field', // custom class
'profile' => true, // show in user profile
'required' => true, // make this field required
'levels' => array(2)
)
);
$fields_for_third[] = new PMProRH_Field(
'third_field', // input name, will also be used as meta key
'text', // type of field
array(
'label' => 'Third Field' , // custom field label
'class' => 'third_field', // custom class
'profile' => true, // show in user profile
'required' => true, // make this field required
)
);
foreach($fields_for_third as $field)
pmprorh_add_registration_field(
'third', // location on checkout page
$field // PMProRH_Field object
);
}
add_action( 'init', 'checkout_boxes_example', 10 );
@heatherdevaun
Copy link

😍 Thank you, Andrew! This is exactly what I needed. You might consider adding a link to this from https://www.paidmembershipspro.com/documentation/register-helper-documentation/adding-fields/ because this is a really excellent example of how Register Helper makes custom forms flexible and where/how to define the checkout box (which is what I was wondering). Thank you so much; I really appreciate you showing how this comes together.

@AnadarPro
Copy link

Is there a way to only add it for certain membership levels?

Thanks.

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