Skip to content

Instantly share code, notes, and snippets.

@vapvarun
Created April 14, 2026 04:05
Show Gist options
  • Select an option

  • Save vapvarun/008dad6d4597db4dc0f3cb33349b54d3 to your computer and use it in GitHub Desktop.

Select an option

Save vapvarun/008dad6d4597db4dc0f3cb33349b54d3 to your computer and use it in GitHub Desktop.
How to Extend the EDD Checkout with Custom Fields (eddsellservices.com)
// 1. Render
add_action( 'edd_purchase_form_after_personal_info', function () {
$value = edd_get_option( 'vat_id_placeholder', '' );
?>
<fieldset id="edd_checkout_vat_id">
<legend><?php esc_html_e( 'VAT ID (optional)', 'mystore' ); ?></legend>
<p>
<label class="edd-label" for="edd-vat-id">
<?php esc_html_e( 'EU VAT Registration Number', 'mystore' ); ?>
</label>
<input type="text" class="edd-input" id="edd-vat-id"
name="edd_vat_id" autocomplete="off" />
<span class="edd-description">
<?php esc_html_e( 'B2B customers in EU countries: enter VAT ID to remove VAT from your purchase.', 'mystore' ); ?>
</span>
</p>
</fieldset>
<?php
} );
// 2. Validate
add_action( 'edd_checkout_error_checks', function ( $valid_data, $post_data ) {
$vat_id = ! empty( $post_data['edd_vat_id'] ) ? trim( $post_data['edd_vat_id'] ) : '';
if ( $vat_id && ! preg_match( '/^[A-Z]{2}[0-9A-Z]{2,12}$/', strtoupper( $vat_id ) ) ) {
edd_set_error( 'invalid_vat_id', __( 'Please enter a valid EU VAT ID (e.g. DE123456789).', 'mystore' ) );
}
}, 10, 2 );
// 3. Save to order
add_action( 'edd_insert_payment', function ( $payment_id, $payment_data ) {
if ( ! empty( $_POST['edd_vat_id'] ) ) {
edd_update_payment_meta(
$payment_id,
'_edd_vat_id',
sanitize_text_field( wp_unslash( $_POST['edd_vat_id'] ) )
);
}
}, 10, 2 );
add_action( 'edd_purchase_form_after_personal_info', function () {
$cart_items = edd_get_cart_contents();
$needs_team_size = false;
foreach ( $cart_items as $item ) {
if ( get_post_meta( $item['id'], '_edd_is_team_license', true ) ) {
$needs_team_size = true;
break;
}
}
if ( ! $needs_team_size ) {
return;
}
?>
<fieldset>
<legend><?php esc_html_e( 'Team Details', 'mystore' ); ?></legend>
<p>
<label for="edd-team-size"><?php esc_html_e( 'Number of seats', 'mystore' ); ?></label>
<input type="number" min="1" max="500" name="edd_team_size"
id="edd-team-size" class="edd-input" required />
</p>
</fieldset>
<?php
} );
(function ( $ ) {
$( document ).ready( function () {
const businessToggle = $( '#edd-is-business' );
const vatFieldset = $( '#edd-vat-fieldset' );
vatFieldset.hide();
businessToggle.on( 'change', function () {
if ( this.checked ) {
vatFieldset.show();
$( '#edd-vat-id' ).attr( 'required', true );
} else {
vatFieldset.hide();
$( '#edd-vat-id' ).removeAttr( 'required' ).val( '' );
}
} );
} );
} )( jQuery );
add_action( 'edd_view_order_details_payment_meta_after', function ( $payment_id ) {
$vat_id = edd_get_payment_meta( $payment_id, '_edd_vat_id' );
$team_size = edd_get_payment_meta( $payment_id, '_edd_team_size' );
if ( ! $vat_id && ! $team_size ) {
return;
}
?>
<div class="edd-order-custom-fields">
<h3><?php esc_html_e( 'Custom Fields', 'mystore' ); ?></h3>
<?php if ( $vat_id ) : ?>
<p><strong>VAT ID:</strong> <?php echo esc_html( $vat_id ); ?></p>
<?php endif; ?>
<?php if ( $team_size ) : ?>
<p><strong>Team Size:</strong> <?php echo esc_html( $team_size ); ?></p>
<?php endif; ?>
</div>
<?php
} );
add_filter( 'edd_cart_tax', function ( $tax, $subtotal ) {
if ( ! empty( $_POST['edd_vat_id'] ) && is_eu_customer() ) {
// B2B reverse-charge, VAT is the buyer's responsibility.
return 0;
}
return $tax;
}, 10, 2 );
add_filter( 'edd_cart_item_price', function ( $price, $item ) {
if ( isset( $_POST['edd_team_size'] ) && (int) $_POST['edd_team_size'] > 1 ) {
$seats = (int) $_POST['edd_team_size'];
return $price * $seats;
}
return $price;
}, 10, 2 );
add_action( 'edd_subscription_post_create', function ( $subscription_id, $subscription ) {
$payment_id = $subscription->parent_payment_id;
$vat_id = edd_get_payment_meta( $payment_id, '_edd_vat_id' );
if ( $vat_id ) {
update_post_meta( $subscription_id, '_vat_id', $vat_id );
}
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment