-
-
Save strangerstudios/1894897 to your computer and use it in GitHub Desktop.
<?php | |
/* | |
Plugin Name: PMPro Shipping | |
Plugin URI: https://gist.github.com/gists/1894897/ | |
Description: Add shipping to the checkout page and other updates. | |
Version: .2.1 | |
Author: Stranger Studios | |
Author URI: http://www.strangerstudios.com | |
Note that this plugin requires PMPro 1.3.19 or higher to function fully. | |
*/ | |
//add a shipping address field to the checkout page with "sameas" checkbox | |
function my_pmpro_checkout_boxes() | |
{ | |
global $pmpro_states, $sfirstname, $slastname, $saddress1, $saddress2, $scity, $sstate, $szipcode, $shipping_address, $pmpro_requirebilling; | |
?> | |
<h2 id="userlogin">Shipping Address</h2> | |
<p style="margin-left:130px; "><input type="checkbox" id="sameasbilling" name="sameasbilling" value="1" <?php if(!empty($sameasbilling)) { ?>checked="checked"<?php } ?> />Ship to the billing address used above. </p> | |
<div id="shipping-fields"> | |
<div> | |
<label for="sfirstname"><span class="red">*</span>First Name</label> | |
<input id="sfirstname" name="sfirstname" type="text" class="input" size="20" value="<?php echo esc_attr($sfirstname);?>" /> | |
</div> | |
<div> | |
<label for="slastname"><span class="red">*</span>Last Name</label> | |
<input id="slastname" name="slastname" type="text" class="input" size="20" value="<?php echo esc_attr($slastname);?>" /> | |
</div> | |
<div> | |
<label for="saddress1"><span class="red">*</span>Address 1</label> | |
<input id="saddress1" name="saddress1" type="text" class="input" size="20" value="<?php echo esc_attr($saddress1);?>" /> | |
</div> | |
<div> | |
<label for="saddress2">Address 2</label> | |
<input id="saddress2" name="saddress2" type="text" class="input" size="20" value="<?php echo esc_attr($saddress2);?>" /> <small class="lite">(optional)</small> | |
</div> | |
<div> | |
<label for="scity_state_zip"><span class="red">*</span>City, State Zip</label> | |
<input id="scity" name="scity" type="text" class="input" size="14" style="width: 125px;" value="<?php echo esc_attr($scity)?>" />, | |
<?php /* | |
<input id="sstate" name="sstate" type="text" class="input" size="2" value="<?php echo esc_attr($sstate)?>" /> | |
*/ ?> | |
<select name="sstate"> | |
<option value="">--</option> | |
<?php | |
$sstate = get_user_meta($user->ID, 'pmpro_sstate', true); | |
foreach($pmpro_states as $ab => $st) | |
{ | |
?> | |
<option value="<?=$ab?>" <?php if($ab == $sstate) { ?>selected="selected"<?php } ?>><?=$st?></option> | |
<?php } ?> | |
</select> | |
<input id="szipcode" name="szipcode" type="text" class="input" size="5" style="width: 75px" value="<?php echo esc_attr($szipcode)?>" /> | |
</div> | |
</div> | |
<?php if($pmpro_requirebilling) { ?> | |
<script> | |
jQuery('#sameasbilling').change(function() { | |
if(jQuery('#sameasbilling').attr('checked')) | |
{ | |
jQuery('#shipping-fields').hide(); | |
} | |
else | |
{ | |
jQuery('#shipping-fields').show(); | |
} | |
}); | |
</script> | |
<?php } ?> | |
<?php | |
} | |
add_action("pmpro_checkout_after_billing_fields", "my_pmpro_checkout_boxes"); | |
//update a user meta value on checkout | |
function my_pmpro_after_checkout($user_id) | |
{ | |
if(!empty($_REQUEST['sameasbilling'])) | |
$sameasbilling = true; //we'll get the fields further down below | |
elseif(!empty($_REQUEST['saddress1'])) | |
{ | |
//grab the fields entered by the user at checkout | |
$sfirstname = $_REQUEST['sfirstname']; | |
$slastname = $_REQUEST['slastname']; | |
$saddress1 = $_REQUEST['saddress1']; | |
if(!empty($_REQUEST['saddress2'])) { | |
$saddress2 = $_REQUEST['saddress2']; | |
} | |
$scity = $_REQUEST['scity']; | |
$sstate = $_REQUEST['sstate']; | |
$szipcode = $_REQUEST['szipcode']; | |
} | |
elseif(!empty($_SESSION['sameasbilling'])) | |
{ | |
//coming back from PayPal. same as billing | |
$sameasbilling = true; | |
unset($_SESSION['sameasbilling']); | |
} | |
elseif(!empty($_SESSION['saddress1'])) | |
{ | |
//coming back from PayPal. grab the fields from session | |
$sfirstname = $_SESSION['sfirstname']; | |
$slastname = $_SESSION['slastname']; | |
$saddress1 = $_SESSION['saddress1']; | |
if(!empty($_SESSION['saddress2'])) { | |
$saddress2 = $_SESSION['saddress2']; | |
} | |
$scity = $_SESSION['scity']; | |
$sstate = $_SESSION['sstate']; | |
$szipcode = $_SESSION['szipcode']; | |
//unset the session vars | |
unset($_SESSION['sfirstname']); | |
unset($_SESSION['slastname']); | |
unset($_SESSION['saddress1']); | |
if(!empty($_SESSION['saddress2'])) { | |
unset($_SESSION['saddress2']); | |
} | |
unset($_SESSION['scity']); | |
unset($_SESSION['sstate']); | |
unset($_SESSION['szipcode']); | |
} | |
if(!empty($sameasbilling)) | |
{ | |
//set the shipping fields to be the same as the billing fields | |
$sfirstname = get_user_meta($user_id, "pmpro_bfirstname", true); | |
$slastname = get_user_meta($user_id, "pmpro_blastname", true); | |
$saddress1 = get_user_meta($user_id, "pmpro_baddress1", true); | |
$saddress2 = get_user_meta($user_id, "pmpro_baddress2", true); | |
$scity = get_user_meta($user_id, "pmpro_bcity", true); | |
$sstate = get_user_meta($user_id, "pmpro_bstate", true); | |
$szipcode = get_user_meta($user_id, "pmpro_bzipcode", true); | |
} | |
if(!empty($saddress1)) | |
{ | |
//update the shipping user meta | |
update_user_meta($user_id, "pmpro_sfirstname", $sfirstname); | |
update_user_meta($user_id, "pmpro_slastname", $slastname); | |
update_user_meta($user_id, "pmpro_saddress1", $saddress1); | |
update_user_meta($user_id, "pmpro_saddress2", $saddress2); | |
update_user_meta($user_id, "pmpro_scity", $scity); | |
update_user_meta($user_id, "pmpro_sstate", $sstate); | |
update_user_meta($user_id, "pmpro_szipcode", $szipcode); | |
} | |
} | |
add_action("pmpro_after_checkout", "my_pmpro_after_checkout"); | |
//show the shipping address in the profile | |
function my_show_extra_profile_fields($user) | |
{ | |
global $pmpro_states; | |
?> | |
<h3>Shipping Address</h3> | |
<table class="form-table"> | |
<tr> | |
<th>First Name</th> | |
<td> | |
<input id="sfirstname" name="sfirstname" type="text" class="regular-text" value="<?php echo esc_attr( get_user_meta($user->ID, 'pmpro_sfirstname', true) ); ?>" /> | |
</td> | |
</tr> | |
<tr> | |
<th>Last Name</th> | |
<td> | |
<input id="slastname" name="slastname" type="text" class="regular-text" value="<?php echo esc_attr( get_user_meta($user->ID, 'pmpro_slastname', true) ); ?>" /> | |
</td> | |
</tr> | |
<tr> | |
<th>Address 1</th> | |
<td> | |
<input id="saddress1" name="saddress1" type="text" class="regular-text" value="<?php echo esc_attr( get_user_meta($user->ID, 'pmpro_saddress1', true) ); ?>" /> | |
</td> | |
</tr> | |
<tr> | |
<th>Address 2</th> | |
<td> | |
<input id="saddress2" name="saddress2" type="text" class="regular-text" value="<?php echo esc_attr( get_user_meta($user->ID, 'pmpro_saddress2', true) ); ?>" /> | |
</td> | |
</tr> | |
<tr> | |
<th>City</th> | |
<td> | |
<input id="scity" name="scity" type="text" class="regular-text" value="<?php echo esc_attr( get_user_meta($user->ID, 'pmpro_scity', true) ); ?>" /> | |
</td> | |
</tr> | |
<tr> | |
<th>State</th> | |
<td> | |
<select id="sstate" name="sstate"> | |
<option value="">--</option> | |
<?php | |
$sstate = get_user_meta($user->ID, 'pmpro_sstate', true); | |
foreach($pmpro_states as $ab => $st) | |
{ | |
?> | |
<option value="<?=$ab?>" <?php if($ab == $sstate) { ?>selected="selected"<?php } ?>><?=$st?></option> | |
<?php } ?> | |
</select> | |
<?php /* | |
<input id="sstate" name="sstate" type="text" class="regular-text" value="<?php echo esc_attr( get_user_meta($user->ID, 'pmpro_sstate', true) ); ?>" /> | |
*/ ?> | |
</td> | |
</tr> | |
<tr> | |
<th>Zip</th> | |
<td> | |
<input id="szipcode" name="szipcode" type="text" class="regular-text" value="<?php echo esc_attr( get_user_meta($user->ID, 'pmpro_szipcode', true) ); ?>" /> | |
</td> | |
</tr> | |
</table> | |
<?php | |
} | |
add_action( 'show_user_profile', 'my_show_extra_profile_fields' ); | |
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' ); | |
function my_save_extra_profile_fields( $user_id ) | |
{ | |
if ( !current_user_can( 'edit_user', $user_id ) ) | |
return false; | |
update_usermeta( $user_id, 'pmpro_sfirstname', $_POST['sfirstname'] ); | |
update_usermeta( $user_id, 'pmpro_slastname', $_POST['slastname'] ); | |
update_usermeta( $user_id, 'pmpro_saddress1', $_POST['saddress1'] ); | |
update_usermeta( $user_id, 'pmpro_saddress2', $_POST['saddress2'] ); | |
update_usermeta( $user_id, 'pmpro_scity', $_POST['scity'] ); | |
update_usermeta( $user_id, 'pmpro_sstate', $_POST['sstate'] ); | |
update_usermeta( $user_id, 'pmpro_szipcode', $_POST['szipcode'] ); | |
} | |
add_action( 'personal_options_update', 'my_save_extra_profile_fields' ); | |
add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' ); | |
/* | |
These bits are required for PayPal Express only. | |
*/ | |
function my_pmpro_paypalexpress_session_vars() | |
{ | |
//save our added fields in session while the user goes off to PayPal | |
$_SESSION['sameasbilling'] = $_REQUEST['sameasbilling']; | |
//assume the request is set | |
$_SESSION['saddress1'] = $_REQUEST['saddress1']; | |
$_SESSION['sfirstname'] = $_REQUEST['sfirstname']; | |
$_SESSION['slastname'] = $_REQUEST['slastname']; | |
$_SESSION['sstate'] = $_REQUEST['sstate']; | |
$_SESSION['scity'] = $_REQUEST['scity']; | |
$_SESSION['szipcode'] = $_REQUEST['szipcode']; | |
//check this one cause it's optional | |
if(!empty($_REQUEST['saddress2'])) | |
$_SESSION['saddress2'] = $_REQUEST['saddress2']; | |
else | |
$_SESSION['saddress2'] = ""; | |
} | |
add_action("pmpro_paypalexpress_session_vars", "my_pmpro_paypalexpress_session_vars"); | |
/* | |
Require the shipping fields (optional) | |
*/ | |
function my_pmpro_registration_checks($okay) | |
{ | |
//only check if we're okay so far | |
if($okay) | |
{ | |
global $pmpro_msg, $pmpro_msgt; | |
if(empty($_REQUEST['sameasbilling']) && (empty($_REQUEST['saddress1']) || empty($_REQUEST['scity']) || empty($_REQUEST['sstate']) || empty($_REQUEST['szipcode']))) | |
{ | |
$pmpro_msg = "Please enter a shipping address, city, state, and zipcode."; | |
$pmpro_msgt = "pmpro_error"; | |
$okay = false; | |
} | |
} | |
return $okay; | |
} | |
add_filter("pmpro_registration_checks", "my_pmpro_registration_checks"); | |
//adding shipping address to confirmation page | |
function my_pmpro_confirmation_message($confirmation_message, $pmpro_invoice) | |
{ | |
global $current_user; | |
//does the user have a shipping address? | |
$sfirstname = get_user_meta($current_user->ID, "pmpro_sfirstname", true); | |
$slastname = get_user_meta($current_user->ID, "pmpro_slastname", true); | |
$saddress1 = get_user_meta($current_user->ID, "pmpro_saddress1", true); | |
$saddress2 = get_user_meta($current_user->ID, "pmpro_saddress2", true); | |
$scity = get_user_meta($current_user->ID, "pmpro_scity", true); | |
$sstate = get_user_meta($current_user->ID, "pmpro_sstate", true); | |
$szipcode = get_user_meta($current_user->ID, "pmpro_szipcode", true); | |
if(!empty($scity) && !empty($sstate)) | |
{ | |
$shipping_address = $sfirstname . " " . $slastname . "<br />" . $saddress1 . "<br />"; | |
if($saddress2) | |
$shipping_address .= $saddress2 . "<br />"; | |
$shipping_address .= $scity . ", " . $sstate . " " . $szipcode; | |
$confirmation_message .= "<br /><h2 id='userlogin'>Shipping Information:<h2><p>" . $shipping_address; | |
} | |
return $confirmation_message; | |
} | |
add_filter("pmpro_confirmation_message", "my_pmpro_confirmation_message", 10, 2); | |
//adding shipping address to confirmation email | |
function my_pmpro_email_body($body, $pmpro_email) | |
{ | |
global $wpdb; | |
//get the user_id from the email | |
$user_id = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_email = '" . $pmpro_email->email . "' LIMIT 1"); | |
if(!empty($user_id)) | |
{ | |
//does the user being emailed have a shipping address? | |
$sfirstname = get_user_meta($user_id, "pmpro_sfirstname", true); | |
$slastname = get_user_meta($user_id, "pmpro_slastname", true); | |
$saddress1 = get_user_meta($user_id, "pmpro_saddress1", true); | |
$saddress2 = get_user_meta($user_id, "pmpro_saddress2", true); | |
$scity = get_user_meta($user_id, "pmpro_scity", true); | |
$sstate = get_user_meta($user_id, "pmpro_sstate", true); | |
$szipcode = get_user_meta($user_id, "pmpro_szipcode", true); | |
if(!empty($scity) && !empty($sstate)) | |
{ | |
$shipping_address = $sfirstname . " " . $slastname . "<br />" . $saddress1 . "<br />"; | |
if($saddress2) | |
$shipping_address .= $saddress2 . "<br />"; | |
$shipping_address .= $scity . ", " . $sstate . " " . $szipcode; | |
} | |
if(!empty($shipping_address)) | |
{ | |
//squeeze the shipping address above the billing information or above the log link | |
if(strpos($body, "Billing Information:")) | |
$body = str_replace("Billing Information:", "Shipping Address:<br />" . $shipping_address . "<br /><br />Billing Information:", $body); | |
else | |
$body = str_replace("Log in to your membership", "Shipping Address:<br />" . $shipping_address . "<br /><br />Log in to your membership", $body); | |
} | |
} | |
return $body; | |
} | |
add_filter("pmpro_email_body", "my_pmpro_email_body", 10, 2); | |
//use a dropdown for state in the billing fields | |
function my_pmpro_state_dropdowns($use) | |
{ | |
return true; | |
} | |
add_filter("pmpro_state_dropdowns", "my_pmpro_state_dropdowns"); | |
?> |
This should be downloaded as a separate file and uploaded to your plugins folder, then activated.
Hi, Great plugin. Working perfectly except when the user does not enter a shipping address is auto-fills with my address. Somehow stored in the system. I am not using paypal on the site - using PMPro to connect to a merchant service.
Can I just comment out lines 84 - 156 ?
Hi, can someone please explain in detail how to apply this step by step? I'm a total newb on this and starting to build my own website.
When I download - what do I name the file and extension?
where do I save it in the plugins folder and how do I activate it?
any help on this will be much appreciated.
thanks
its ok - managed to suss it.
How can i remove the first and last name fields? I dont really need those as the PMpro default form will already collect this information
Which file should this be added to? Checkout.php?