Skip to content

Instantly share code, notes, and snippets.

@asharirfan
Created April 18, 2016 20:16
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 asharirfan/30a382894b5a62f2c9f8941d250529ea to your computer and use it in GitHub Desktop.
Save asharirfan/30a382894b5a62f2c9f8941d250529ea to your computer and use it in GitHub Desktop.
This gist is used to create custom fields in the checkout form of EDD (Easy Digital Downloads).
<?php
/**
* This gist is used to create custom fields in the
* checkout form of EDD (Easy Digital Downloads).
*/
/**
* Adding HTML for User Contact Form.
*/
function add_address_html() {
?>
<p id="edd-address-wrap">
<label class="edd-label" for="edd-address">
<?php _e( 'Delivery Address', 'edd' ); ?>
<span class="edd-required-indicator">*</span>
</label>
<span class="edd-description">
<?php _e( 'We will deliver this product to this address.', 'edd' ); ?>
</span>
<textarea class="edd-input required" rows="6" name="edd_address" id="edd-address" placeholder="<?php _e( 'Delivery Address', 'edd' ); ?>" value=""></textarea>
</p>
<?php
}
add_action( 'edd_purchase_form_user_info', 'add_address_html' );
/**
* Make address field required.
*/
function make_address_required( $required_fields ) {
$required_fields = array(
'edd_address' => array(
'error_id' => 'invalid_address',
'error_message' => 'Please enter a valid delivery address.'
),
);
return $required_fields;
}
add_filter( 'edd_purchase_form_required_fields', 'make_address_required' );
/**
* Check if address field is empty.
*/
function check_address_empty( $valid_data, $data ) {
if ( empty( $data['edd_address'] ) ) {
edd_set_error( 'invalid_address', 'Please enter your delivery address.' );
}
}
add_action( 'edd_checkout_error_checks', 'check_address_empty', 10, 2 );
/**
* Store the address in the payment meta.
*/
function store_address_meta( $payment_meta ) {
$payment_meta['address'] = isset($_POST['edd_address']) ? $_POST['edd_address'] : '';
return $payment_meta;
}
add_filter( 'edd_payment_meta', 'store_address_meta' );
/**
* Show the address in the "View Order Details" popup.
*/
function address_edit_purchase_details( $payment_meta, $user_info ) {
$address = isset( $payment_meta['address'] ) ? $payment_meta['address'] : 'none';
?>
<div class="column-container customer-info">
<div class="column">
<p><strong><?php echo __( 'Delivery Address:', 'pippin' ); ?></strong></p>
<p><?php printf( '%s', $address ); ?></p>
</div>
</div>
<?php
}
add_action( 'edd_payment_personal_details_list', 'address_edit_purchase_details', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment