Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Only show address fields for paid levels with offsite gateways in Paid Memberships Pro...otherwise only show first and last name.
<?php
/*
Plugin Name: Paid Memberships Pro - Address For Offsite Gateways Add On - Modified
Plugin URI: http://www.paidmembershipspro.com/wp/pmpro-address-for-free-levels/
Description: Only show address fields for paid levels with offsite gateways in Paid Memberships Pro...otherwise only show first and last name.
Based on: https://github.com/strangerstudios/pmpro-add-name-to-checkout and
http://www.paidmembershipspro.com/2015/03/collect-address-for-free-levels-or-for-offsite-checkout-gateways/
Version: .3
Author: Stranger Studios
Author URI: http://www.strangerstudios.com
*/
/*
Show address fields for free levels too.
Add this code to your active theme's functions.php or a custom plugin.
*/
//keep address fields shown on checkout page
function pmproaffl_pmpro_checkout_boxes_require_address()
{
//don't force show them on review page
global $pmpro_review, $pmpro_level;
if(!pmpro_isLevelFree($pmpro_level))
{
?>
<script>
var pmpro_show_billing_address_fields_timer;
function showBillingAddressFields()
{
<?php if(empty($pmpro_review)) { ?>
jQuery('#pmpro_billing_address_fields').show();
<?php } else { ?>
jQuery('#pmpro_billing_address_fields').hide(); //hiding when on the review page
<?php } ?>
pmpro_show_billing_address_fields_timer = setTimeout(function(){showBillingAddressFields();}, 200);
}
jQuery(document).ready(function() {
//show it and keep showing it
showBillingAddressFields();
<?php
//remove billing from address title if the level is free
if(pmpro_isLevelFree($pmpro_level))
{
?>
//change heading
jQuery('#pmpro_billing_address_fields th').html('Address');
<?php
}
?>
});
</script><?php
}
else
{
if(isset($_REQUEST['first_name']))
$first_name = $_REQUEST['first_name'];
else
$first_name = "";
if(isset($_REQUEST['last_name']))
$last_name = $_REQUEST['last_name'];
else
$last_name = "";
?>
<div>
<label for="first_name">First Name</label>
<input id="first_name" name="first_name" type="text" class="input pmpro_required" size="30" value="<?=$first_name?>" />
</div>
<div>
<label for="last_name">Last Name</label>
<input id="last_name" name="last_name" type="text" class="input pmpro_required" size="30" value="<?=$last_name?>" />
</div> <?php
}
}
add_action("pmpro_checkout_boxes", "pmproaffl_pmpro_checkout_boxes_require_address");
//make sure we include address fields (for post 1.8)
function pmproaffl_init_include_address_fields_at_checkout()
{
add_filter('pmpro_include_billing_address_fields', '__return_true');
}
add_action('init', 'pmproaffl_init_include_address_fields_at_checkout', 30);
//make sure address fields are required
function pmproaffl_pmpro_required_user_fields($fields)
{
global $bfirstname, $blastname, $baddress1, $bcity, $bstate, $bzipcode, $bcountry, $bphone, $bemail, $pmpro_level;
if(!pmpro_isLevelFree($pmpro_level))
{
$fields["bfirstname"] = $bfirstname;
$fields["blastname"] = $blastname;
$fields["baddress1"] = $baddress1;
$fields["bcity"] = $bcity;
$fields["bstate"] = $bstate;
$fields["bzipcode"] = $bzipcode;
$fields["bphone"] = $bphone;
$fields["bemail"] = $bemail;
$fields["bcountry"] = $bcountry;
}
return $fields;
}
add_action("pmpro_required_user_fields", "pmproaffl_pmpro_required_user_fields");
//save fields in session for PayPal Express/etc
function pmproaffl_pmpro_paypalexpress_session_vars()
{
//assume the request is set
$_SESSION['bfirstname'] = $_REQUEST['bfirstname'];
$_SESSION['blastname'] = $_REQUEST['blastname'];
$_SESSION['baddress1'] = $_REQUEST['baddress1'];
$_SESSION['bcity'] = $_REQUEST['bcity'];
$_SESSION['bstate'] = $_REQUEST['bstate'];
$_SESSION['bzipcode'] = $_REQUEST['bzipcode'];
$_SESSION['bphone'] = $_REQUEST['bphone'];
$_SESSION['bemail'] = $_REQUEST['bemail'];
$_SESSION['bcountry'] = $_REQUEST['bcountry'];
//check this one cause it's optional
if(!empty($_REQUEST['baddress2']))
$_SESSION['baddress2'] = $_REQUEST['baddress2'];
else
$_SESSION['baddress2'] = "";
//if there is a user here, save in user meta as well
global $current_user;
if(!empty($current_user->ID))
{
//save billing info ect, as user meta
$meta_keys = array("pmpro_bfirstname", "pmpro_blastname", "pmpro_baddress1", "pmpro_baddress2", "pmpro_bcity", "pmpro_bstate", "pmpro_bzipcode", "pmpro_bcountry", "pmpro_bphone", "pmpro_bemail");
$meta_values = array($_REQUEST['bfirstname'], $_REQUEST['blastname'], $_REQUEST['baddress1'], $_REQUEST['baddress2'], $_REQUEST['bcity'], $_REQUEST['bstate'], $_REQUEST['bzipcode'], $_REQUEST['bcountry'], $_REQUEST['bphone'], $_REQUEST['bemail']);
pmpro_replaceUserMeta($current_user->ID, $meta_keys, $meta_values);
}
}
add_action("pmpro_paypalexpress_session_vars", "pmproaffl_pmpro_paypalexpress_session_vars");
//Load fields from session if available.
function pmproaffl_init_load_session_vars($param)
{
//check that no field values were passed in and that we have some in session
if(empty($_REQUEST['bfirstname']) && !empty($_SESSION['bfirstname']))
{
$_REQUEST['bfirstname'] = $_SESSION['bfirstname'];
$_REQUEST['blastname'] = $_SESSION['blastname'];
$_REQUEST['baddress1'] = $_SESSION['baddress1'];
$_REQUEST['baddress2'] = $_SESSION['baddress2'];
$_REQUEST['bcity'] = $_SESSION['bcity'];
$_REQUEST['bstate'] = $_SESSION['bstate'];
$_REQUEST['bzipcode'] = $_SESSION['bzipcode'];
$_REQUEST['bphone'] = $_SESSION['bphone'];
$_REQUEST['bemail'] = $_SESSION['bemail'];
$_REQUEST['bcountry'] = $_SESSION['bcountry'];
}
return $param;
}
add_action('init', 'pmproaffl_init_load_session_vars', 5);
//after checkout, clear any session vars
function pmproaffl_pmpro_after_checkout()
{
global $pmpro_level;
$vars = array('bfirstname', 'blastname', 'baddress1', 'baddress2', 'bcity', 'bstate', 'bzipcode', 'bphone', 'bemail', 'bcountry');
foreach($vars as $var)
{
if(isset($_SESSION[$var]))
unset($_SESSION[$var]);
}
if(pmpro_isLevelFree($pmpro_level))
{
if(!empty($_REQUEST['first_name']))
$first_name = $_REQUEST['first_name'];
elseif(!empty($_SESSION['first_name']))
$first_name = $_SESSION['first_name'];
else
$first_name = "";
if(!empty($_REQUEST['last_name']))
$last_name = $_REQUEST['last_name'];
elseif(!empty($_SESSION['last_name']))
$last_name = $_SESSION['last_name'];
else
$last_name = "";
update_user_meta($user_id, "first_name", $first_name);
update_user_meta($user_id, "last_name", $last_name);
}
}
add_action("pmpro_after_checkout", "pmproaffl_pmpro_after_checkout");
/*
Function to add links to the plugin row meta
*/
function pmproaffl_plugin_row_meta($links, $file) {
if(strpos($file, 'pmpro-address-for-free-levels.php') !== false)
{
$new_links = array(
'<a href="' . esc_url('http://www.paidmembershipspro.com/add-ons/code-gists/pmpro-require-nameaddress-for-free-level/') . '" title="' . esc_attr( __( 'View Documentation', 'pmpro' ) ) . '">' . __( 'Docs', 'pmpro' ) . '</a>',
'<a href="' . esc_url('http://paidmembershipspro.com/support/') . '" title="' . esc_attr( __( 'Visit Customer Support Forum', 'pmpro' ) ) . '">' . __( 'Support', 'pmpro' ) . '</a>',
);
$links = array_merge($links, $new_links);
}
return $links;
}
add_filter('plugin_row_meta', 'pmproaffl_plugin_row_meta', 10, 2);
//add the fields to the form
//require the fields
function pmproan2c_pmpro_registration_checks()
{
if(!empty($_REQUEST['bfirstname']) && !empty($_REQUEST['blastname']))
{
return true;
}
global $pmpro_msg, $pmpro_msgt, $current_user;
if(!empty($_REQUEST['first_name']))
$first_name = $_REQUEST['first_name'];
elseif(!empty($_SESSION['first_name']))
$first_name = $_SESSION['first_name'];
else
$first_name = "";
if(!empty($_REQUEST['last_name']))
$last_name = $_REQUEST['last_name'];
elseif(!empty($_SESSION['last_name']))
$last_name = $_SESSION['last_name'];
else
$last_name = "";
if($first_name && $last_name || $current_user->ID)
{
//all good
return true;
}
else
{
$pmpro_msg = "The first and last name fields are required.";
$pmpro_msgt = "pmpro_error";
return false;
}
}
add_filter("pmpro_registration_checks", "pmproan2c_pmpro_registration_checks");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.