Skip to content

Instantly share code, notes, and snippets.

@SeanTOSCD
Last active September 5, 2024 01:48
Show Gist options
  • Save SeanTOSCD/40dc724372a03160db33 to your computer and use it in GitHub Desktop.
Save SeanTOSCD/40dc724372a03160db33 to your computer and use it in GitHub Desktop.
EDD - Billing Details Control
<?php // DO NOT COPY THIS LINE
/**
* remove default billing details form fields
*/
function custom_edd_remove_default_fields() {
remove_action( 'edd_after_cc_fields', 'edd_default_cc_address_fields' );
}
add_action( 'init', 'custom_edd_remove_default_fields' );
/**
* manually add billing details form fields
*/
function sd_edd_default_cc_address_fields() {
$logged_in = is_user_logged_in();
$customer = EDD()->session->get( 'customer' );
$customer = wp_parse_args( $customer, array( 'address' => array(
'line1' => '',
'line2' => '',
'city' => '',
'zip' => '',
'state' => '',
'country' => ''
) ) );
$customer['address'] = array_map( 'sanitize_text_field', $customer['address'] );
if( $logged_in ) {
$user_address = get_user_meta( get_current_user_id(), '_edd_user_address', true );
foreach( $customer['address'] as $key => $field ) {
if ( empty( $field ) && ! empty( $user_address[ $key ] ) ) {
$customer['address'][ $key ] = $user_address[ $key ];
} else {
$customer['address'][ $key ] = '';
}
}
}
ob_start(); ?>
<fieldset id="edd_cc_address" class="cc-address">
<span><legend><?php _e( 'Billing Details', 'edd' ); ?></legend></span>
<?php do_action( 'edd_cc_billing_top' ); ?>
<p id="edd-card-address-wrap">
<label for="card_address" class="edd-label">
<?php _e( 'Billing Address', 'edd' ); ?>
<?php if( edd_field_is_required( 'card_address' ) ) { ?>
<span class="edd-required-indicator">*</span>
<?php } ?>
</label>
<span class="edd-description"><?php _e( 'The primary billing address for your credit card.', 'edd' ); ?></span>
<input type="text" id="card_address" name="card_address" class="card-address edd-input<?php if( edd_field_is_required( 'card_address' ) ) { echo ' required'; } ?>" placeholder="<?php _e( 'Address line 1', 'edd' ); ?>" value="<?php echo $customer['address']['line1']; ?>"/>
</p>
<p id="edd-card-address-2-wrap">
<label for="card_address_2" class="edd-label">
<?php _e( 'Billing Address Line 2 (optional)', 'edd' ); ?>
<?php if( edd_field_is_required( 'card_address_2' ) ) { ?>
<span class="edd-required-indicator">*</span>
<?php } ?>
</label>
<span class="edd-description"><?php _e( 'The suite, apt no, PO box, etc, associated with your billing address.', 'edd' ); ?></span>
<input type="text" id="card_address_2" name="card_address_2" class="card-address-2 edd-input<?php if( edd_field_is_required( 'card_address_2' ) ) { echo ' required'; } ?>" placeholder="<?php _e( 'Address line 2', 'edd' ); ?>" value="<?php echo $customer['address']['line2']; ?>"/>
</p>
<p id="edd-card-city-wrap">
<label for="card_city" class="edd-label">
<?php _e( 'Billing City', 'edd' ); ?>
<?php if( edd_field_is_required( 'card_city' ) ) { ?>
<span class="edd-required-indicator">*</span>
<?php } ?>
</label>
<span class="edd-description"><?php _e( 'The city for your billing address.', 'edd' ); ?></span>
<input type="text" id="card_city" name="card_city" class="card-city edd-input<?php if( edd_field_is_required( 'card_city' ) ) { echo ' required'; } ?>" placeholder="<?php _e( 'City', 'edd' ); ?>" value="<?php echo $customer['address']['city']; ?>"/>
</p>
<p id="edd-card-zip-wrap">
<label for="card_zip" class="edd-label">
<?php _e( 'Billing Zip / Postal Code', 'edd' ); ?>
<?php if( edd_field_is_required( 'card_zip' ) ) { ?>
<span class="edd-required-indicator">*</span>
<?php } ?>
</label>
<span class="edd-description"><?php _e( 'The zip or postal code for your billing address.', 'edd' ); ?></span>
<input type="text" size="4" name="card_zip" class="card-zip edd-input<?php if( edd_field_is_required( 'card_zip' ) ) { echo ' required'; } ?>" placeholder="<?php _e( 'Zip / Postal code', 'edd' ); ?>" value="<?php echo $customer['address']['zip']; ?>"/>
</p>
<p id="edd-card-country-wrap">
<label for="billing_country" class="edd-label">
<?php _e( 'Billing Country', 'edd' ); ?>
<?php if( edd_field_is_required( 'billing_country' ) ) { ?>
<span class="edd-required-indicator">*</span>
<?php } ?>
</label>
<span class="edd-description"><?php _e( 'The country for your billing address.', 'edd' ); ?></span>
<select name="billing_country" id="billing_country" class="billing_country edd-select<?php if( edd_field_is_required( 'billing_country' ) ) { echo ' required'; } ?>">
<?php
$selected_country = edd_get_shop_country();
if( ! empty( $customer['address']['country'] ) && '*' !== $customer['address']['country'] ) {
$selected_country = $customer['address']['country'];
}
$countries = edd_get_country_list();
foreach( $countries as $country_code => $country ) {
echo '<option value="' . esc_attr( $country_code ) . '"' . selected( $country_code, $selected_country, false ) . '>' . $country . '</option>';
}
?>
</select>
</p>
<p id="edd-card-state-wrap">
<label for="card_state" class="edd-label">
<?php _e( 'Billing State / Province', 'edd' ); ?>
<?php if( edd_field_is_required( 'card_state' ) ) { ?>
<span class="edd-required-indicator">*</span>
<?php } ?>
</label>
<span class="edd-description"><?php _e( 'The state or province for your billing address.', 'edd' ); ?></span>
<?php
$selected_state = edd_get_shop_state();
$states = edd_get_shop_states( $selected_country );
if( ! empty( $customer['address']['state'] ) ) {
$selected_state = $customer['address']['state'];
}
if( ! empty( $states ) ) : ?>
<select name="card_state" id="card_state" class="card_state edd-select<?php if( edd_field_is_required( 'card_state' ) ) { echo ' required'; } ?>">
<?php
foreach( $states as $state_code => $state ) {
echo '<option value="' . $state_code . '"' . selected( $state_code, $selected_state, false ) . '>' . $state . '</option>';
}
?>
</select>
<?php else : ?>
<input type="text" size="6" name="card_state" id="card_state" class="card_state edd-input" placeholder="<?php _e( 'State / Province', 'edd' ); ?>"/>
<?php endif; ?>
</p>
<?php do_action( 'edd_cc_billing_bottom' ); ?>
</fieldset>
<?php
echo ob_get_clean();
}
add_action( 'edd_after_cc_fields', 'sd_edd_default_cc_address_fields' );
@SeanTOSCD
Copy link
Author


/**
 * remove billing details for taxation
 */
remove_action( 'edd_purchase_form_after_cc_form', 'edd_checkout_tax_fields', 999 );


/**
 * manually add billing details for taxation
 */
function custom_edd_checkout_tax_fields() {
    if( edd_cart_needs_tax_address_fields() && edd_get_cart_total() )
        sd_edd_default_cc_address_fields();
}
add_action( 'edd_purchase_form_after_cc_form', 'custom_edd_checkout_tax_fields', 999 );```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment