Forked from andrewlimaza/add-billing-to-add-member-and-profile.php
Last active
December 2, 2022 04:29
-
-
Save ipokkel/08cc6d914947ca2d37d86acb4f599c79 to your computer and use it in GitHub Desktop.
Add Paid Memberships Pro Billing Address as required fields to Edit Profile, User Profile Edit (admin), and Add Member from Admin pages. #pmpro-register-helper
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 will add billing fields to Add Member from Admin and the user profile edit pages. | |
* | |
* Optional: Set if fields should be required | |
* | |
* Requires Paid Memberships Pro and PMPro Register Helper plugins active. | |
* | |
* 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/ | |
*/ | |
function add_pmpro_billing_fields_to_edit_profile_and_add_member() { | |
#-- SETTINGS ---# | |
/* Should the fields be required? */ | |
$required = true; // (Boolean) true: fields are required | false: fields are optional. | |
#-- END SETTINGS ---# | |
/* That's it, no further editing required */ | |
// Require PMPro and PMPro Register Helper | |
if ( ! defined( 'PMPRO_VERSION' ) || ! defined( 'PMPRORH_VERSION' ) ) { | |
return; | |
} | |
// Get countries. | |
global $pmpro_countries; | |
// Create an array for the fields. | |
$fields = array(); | |
// Define the fields | |
// Add a Billing Address title for Add Member from Admin page. | |
if ( is_admin() && isset( $_REQUEST['page'] ) && 'pmpro-addmember' == $_REQUEST['page'] ) { | |
$fields[] = new PMProRH_Field( | |
'addmember_billing_title', | |
'html', | |
array( | |
'label' => '<strong>Billing Address</strong>', | |
'profile' => 'only_admin', | |
'addmember' => true, | |
) | |
); | |
} | |
// List the fields you want to include. | |
$address_fields = array( | |
'pmpro_bfirstname' => 'First Name', | |
'pmpro_blastname' => 'Last Name', | |
'pmpro_baddress1' => 'Address 1', | |
'pmpro_baddress2' => 'Address 2', | |
'pmpro_bcity' => 'City', | |
'pmpro_bstate' => 'State', | |
'pmpro_bzipcode' => 'Zipcode', | |
'pmpro_bcountry' => 'Country', | |
'pmpro_bphone' => 'Phone', | |
); | |
// Build the fields. | |
foreach ( $address_fields as $name => $label ) { | |
// Set field options for country field. | |
$options = 'pmpro_bcountry' === $name ? $pmpro_countries : array(); | |
$type = 'pmpro_bcountry' === $name ? 'select' : 'text'; | |
// Should the field be required? | |
$required_option = 'pmpro_baddress2' !== $name ? $required : false; // Address 2 is always optional. | |
// Add HTML required attribute to check field before submit. | |
$required_html = $required_option ? array( 'required' => 'required' ) : array(); | |
$fields[] = new PMProRH_Field( | |
$name, | |
$type, | |
array( | |
'label' => $label, | |
'size' => 40, | |
'profile' => 'only', | |
'options' => $options, | |
'addmember' => true, | |
'required' => $required_option, | |
'html_attributes' => $required_html, | |
) | |
); | |
} | |
// Add new checkout box with label. | |
pmprorh_add_checkout_box( 'billing_address_profile', 'Billing Address' ); | |
// Add fields into the custom checkout boxes area created. | |
foreach ( $fields as $field ) { | |
pmprorh_add_registration_field( | |
'billing_address_profile', // location on checkout page | |
$field // PMProRH_Field object | |
); | |
} | |
} | |
add_action( 'init', 'add_pmpro_billing_fields_to_edit_profile_and_add_member' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To add billing address with User Fields: https://gist.github.com/ipokkel/47a8de3553e84bfe08d079d2952c8486