Skip to content

Instantly share code, notes, and snippets.

@riceguitar
Last active December 5, 2015 15:14
Show Gist options
  • Save riceguitar/54fb5ebb7ace5a5d4555 to your computer and use it in GitHub Desktop.
Save riceguitar/54fb5ebb7ace5a5d4555 to your computer and use it in GitHub Desktop.
<?php
/**
* Require last name for EDD purchases
**/
function pw_edd_purchase_form_required_fields( $required_fields ) {
$required_fields['edd_last'] = array(
'error_id' => 'invalid_last_name',
'error_message' => __( 'Please enter your last name.', 'edd' )
);
return $required_fields;
}
add_filter( 'edd_purchase_form_required_fields', 'pw_edd_purchase_form_required_fields' );
/**
* Remove original EDD personal fields
**/
remove_action( 'edd_purchase_form_after_user_info', 'edd_user_info_fields' );
remove_action( 'edd_register_fields_before', 'edd_user_info_fields' );
/**
* New EDD personal fields
**/
function new_edd_user_info_fields() {
$customer = EDD()->session->get( 'customer' );
$customer = wp_parse_args( $customer, array( 'first_name' => '', 'last_name' => '', 'email' => '' ) );
if( is_user_logged_in() ) {
$user_data = get_userdata( get_current_user_id() );
foreach( $customer as $key => $field ) {
if ( 'email' == $key && empty( $field ) ) {
$customer[ $key ] = $user_data->user_email;
} elseif ( empty( $field ) ) {
$customer[ $key ] = $user_data->$key;
}
}
}
$customer = array_map( 'sanitize_text_field', $customer );
?>
<fieldset id="edd_checkout_user_info">
<span><legend><?php echo apply_filters( 'edd_checkout_personal_info_text', __( 'Personal Info', 'easy-digital-downloads' ) ); ?></legend></span>
<p id="edd-first-name-wrap">
<label class="edd-label" for="edd-first">
<?php _e( 'First Name', 'easy-digital-downloads' ); ?>
<?php if( edd_field_is_required( 'edd_first' ) ) { ?>
<span class="edd-required-indicator">*</span>
<?php } ?>
</label>
<input class="edd-input required" type="text" name="edd_first" placeholder="<?php _e( 'First name', 'easy-digital-downloads' ); ?>" id="edd-first" value="<?php echo esc_attr( $customer['first_name'] ); ?>"/>
</p>
<p id="edd-last-name-wrap">
<label class="edd-label" for="edd-last">
<?php _e( 'Last Name', 'easy-digital-downloads' ); ?>
<?php if( edd_field_is_required( 'edd_last' ) ) { ?>
<span class="edd-required-indicator">*</span>
<?php } ?>
</label>
<input class="edd-input<?php if( edd_field_is_required( 'edd_last' ) ) { echo ' required'; } ?>" type="text" name="edd_last" id="edd-last" placeholder="<?php _e( 'Last name', 'easy-digital-downloads' ); ?>" value="<?php echo esc_attr( $customer['last_name'] ); ?>"/>
</p>
<?php do_action( 'edd_purchase_form_before_email' ); ?>
<p id="edd-email-wrap">
<label class="edd-label" for="edd-email">
<?php _e( 'Email Address', 'easy-digital-downloads' ); ?>
<?php if( edd_field_is_required( 'edd_email' ) ) { ?>
<span class="edd-required-indicator">*</span>
<?php } ?>
</label>
<span class="edd-description"><?php _e( 'We will send the purchase receipt to this address.', 'easy-digital-downloads' ); ?></span>
<input class="edd-input required" type="email" name="edd_email" placeholder="<?php _e( 'Email address', 'easy-digital-downloads' ); ?>" id="edd-email" value="<?php echo esc_attr( $customer['email'] ); ?>"/>
</p>
<?php do_action( 'edd_purchase_form_after_email' ); ?>
<?php do_action( 'edd_purchase_form_user_info' ); ?>
<?php do_action( 'edd_purchase_form_user_info_fields' ); ?>
</fieldset>
<?php
}
add_action( 'edd_purchase_form_after_user_info', 'new_edd_user_info_fields' );
add_action( 'edd_register_fields_before', 'new_edd_user_info_fields' );
/**
* Remove original EDD payment icons
**/
//remove_action( 'edd_payment_mode_top', 'edd_show_payment_icons' );
remove_action( 'edd_checkout_form_top', 'edd_show_payment_icons' );
/**
* Remove original EDD credit card function
**/
//add_action( 'edd_new_icons', 'edd_show_payment_icons' );
function obt_edds_credit_card_form( $echo = true ) {
global $edd_options;
ob_start(); ?>
<?php if ( ! wp_script_is ( 'stripe-js' ) ) : ?>
<?php edd_stripe_js( true ); ?>
<?php endif; ?>
<?php do_action( 'edd_before_cc_fields' ); ?>
<fieldset id="edd_cc_fields" class="edd-do-validate">
<span><legend><?php _e( 'Credit Card Info', 'edd' ); ?></legend></span>
<?php if( is_ssl() ) : ?>
<div id="edd_secure_site_wrapper">
<span class="padlock"></span>
<span><?php _e( 'This is a secure SSL encrypted payment.', 'edd' ); ?></span>
</div>
<?php endif; ?>
<p id="edd-card-name-wrap">
<label for="card_name" class="edd-label">
<?php _e( 'Name on the Card', 'edd' ); ?>
<span class="edd-required-indicator">*</span>
</label>
<span class="edd-description"><?php _e( 'The name printed on the front of your credit card.', 'edd' ); ?></span>
<input type="text" autocomplete="off" <?php if ( isset( $edd_options['stripe_js_fallback'] ) ) { echo 'name="card_name" '; } ?>id="card_name" class="card-name edd-input required" placeholder="<?php _e( 'Card name', 'edd' ); ?>" />
</p>
<p id="edd-card-number-wrap">
<label for="card_number" class="edd-label">
<?php _e( 'Card Number', 'edd' ); ?>
<span class="edd-required-indicator">*</span>
<span class="card-type"></span>
</label>
<span class="edd-description"><?php _e( 'The (typically) 16 digits on the front of your credit card.', 'edd' ); ?></span>
<input type="text" autocomplete="off" <?php if ( isset( $edd_options['stripe_js_fallback'] ) ) { echo 'name="card_number" '; } ?>id="card_number" class="card-number edd-input required" placeholder="<?php _e( 'Card number', 'edd' ); ?>" />
</p>
<?php do_action( 'edd_before_cc_expiration' ); ?>
<p class="card-expiration">
<label for="card_exp_month" class="edd-label">
<?php _e( 'Expiration (MM/YY)', 'edd' ); ?>
<span class="edd-required-indicator">*</span>
</label>
<span class="edd-description"><?php _e( 'The date your credit card expires, typically on the front of the card.', 'edd' ); ?></span>
<select <?php if ( isset( $edd_options['stripe_js_fallback'] ) ) { echo 'name="card_exp_month" '; } ?>id="card_exp_month" class="card-expiry-month edd-select edd-select-small required">
<?php for( $i = 1; $i <= 12; $i++ ) { echo '<option value="' . $i . '">' . sprintf ('%02d', $i ) . '</option>'; } ?>
</select>
<span class="exp-divider"> / </span>
<select <?php if ( isset( $edd_options['stripe_js_fallback'] ) ) { echo 'name="card_exp_year" '; } ?>id="card_exp_year" class="card-expiry-year edd-select edd-select-small required">
<?php for( $i = date('Y'); $i <= date('Y') + 30; $i++ ) { echo '<option value="' . $i . '">' . substr( $i, 2 ) . '</option>'; } ?>
</select>
</p>
<?php do_action( 'edd_after_cc_expiration' ); ?>
<p id="edd-card-cvc-wrap">
<label for="card_cvc" class="edd-label">
<?php _e( 'CVC', 'edd' ); ?>
<span class="edd-required-indicator">*</span>
</label>
<span class="edd-description"><?php _e( 'The 3 digit (back) or 4 digit (front) value on your card.', 'edd' ); ?></span>
<input type="text" size="4" autocomplete="off" <?php if ( isset( $edd_options['stripe_js_fallback'] ) ) { echo 'name="card_cvc" '; } ?>id="card_cvc" class="card-cvc edd-input required" placeholder="<?php _e( 'Security code', 'edd' ); ?>" />
</p>
</fieldset>
<?php
do_action( 'edd_after_cc_fields' );
$form = ob_get_clean();
if ( false !== $echo ) {
echo $form;
}
return $form;
}
remove_action( 'edd_stripe_cc_form', 'edds_credit_card_form' );
add_action( 'edd_stripe_cc_form', 'obt_edds_credit_card_form' );
?>
<!-- 237 -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment