Skip to content

Instantly share code, notes, and snippets.

@pippinsplugins
Created July 30, 2013 15:42
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 pippinsplugins/6114113 to your computer and use it in GitHub Desktop.
Save pippinsplugins/6114113 to your computer and use it in GitHub Desktop.
<?php
/*
* Plugin Name: Custom EDD Checkout Fields
*/
// output our custom field HTML
function pippin_edd_custom_checkout_fields() {
?>
<p id="edd-phone-wrap">
<label class="edd-label" for="edd-phone"><?php _e('Telephone Number', 'pippin_edd'); ?><span class="edd-required-indicator">*</span></label>
<span class="edd-description"><?php _e( 'Enter your phone number so we can get in touch with you.', 'pippin_edd' ); ?></span>
<input class="edd-input" type="text" name="edd_phone" id="edd-phone" placeholder="<?php _e('Contact Number', 'pippin_edd'); ?>" value=""/>
</p>
<p id="edd-phone-wrap">
<label class="edd-label" for="edd-website"><?php _e('Company Website', 'pippin_edd'); ?><span class="edd-required-indicator">*</span></label>
<span class="edd-description"><?php _e( 'Enter the name of your website.', 'pippin_edd' ); ?></span>
<input class="edd-input" type="text" name="edd_website" id="edd-website" placeholder="<?php _e('http://', 'pippin_edd'); ?>" value=""/>
</p>
<?php
}
add_action('edd_purchase_form_user_info', 'pippin_edd_custom_checkout_fields');
// check for errors with out custom fields
function pippin_edd_validate_custom_fields($valid_data, $data) {
if( empty( $data['edd_phone'] ) ) {
// check for a phone number
edd_set_error( 'invalid_phone', __('Please provide your phone number.', 'pippin_edd') );
}
if( empty( $data['edd_website'] ) ) {
// check for a phone number
edd_set_error( 'invalid_website', __('Please provide a website name.', 'pippin_edd') );
}
}
add_action('edd_checkout_error_checks', 'pippin_edd_validate_custom_fields', 10, 2);
// store the custom field data in the payment meta
function pippin_edd_store_custom_fields($payment_meta) {
$payment_meta['phone'] = isset( $_POST['edd_phone'] ) ? sanitize_text_field( $_POST['edd_phone'] ) : '';
$payment_meta['website'] = isset( $_POST['edd_website'] ) ? sanitize_text_field( $_POST['edd_website'] ) : '';
return $payment_meta;
}
add_filter('edd_payment_meta', 'pippin_edd_store_custom_fields');
// show the custom fields in the "View Order Details" popup
function pippin_edd_purchase_details($payment_meta, $user_info) {
$phone = isset( $payment_meta['phone'] ) ? $payment_meta['phone'] : 'none';
$website = isset( $payment_meta['website'] ) ? $payment_meta['website'] : 'none';
?>
<li><?php echo __('Phone:', 'pippin_edd') . ' ' . $phone; ?></li>
<li><?php echo __('Website:', 'pippin_edd') . ' ' . $website; ?></li>
<?php
}
add_action('edd_payment_personal_details_list', 'pippin_edd_purchase_details', 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment