Created
January 27, 2017 08:02
-
-
Save kprajapatii/9ccf874ef416aca692351f237367f9f7 to your computer and use it in GitHub Desktop.
Adding custom fields to the checkout form is easy to do with just a couple of functions. The code below will allow you to add two additional fields, one for Alternate Phone and Company Short Description.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Adding a custom field to the Invoicing checkout form. | |
* | |
* Covers: | |
* | |
* Adding the alternate phone & company short description fields to the checkout. | |
* Making the alternate phone field mandatory. | |
* Setting an error when the alternate phone field is empty. | |
* Storing the alternate phone & company short description fields into the invoice payment meta. | |
* Adding the alternate phone & company short description to the invoice view/print page. | |
* Adding the alternate phone & company short description to the emails. | |
*/ | |
// Display custom fields(alternate phone, company short description) under billing address section in checkout form. | |
function wpi_custom_display_checkout_billing_fields( $billing_details ) { | |
$alternate_phone = isset( $billing_details['alternate_phone'] ) ? esc_attr( $billing_details['alternate_phone'] ) : ''; | |
$company_desc = isset( $billing_details['company_desc'] ) ? esc_attr( $billing_details['company_desc'] ) : ''; | |
?> | |
<p class="wpi-cart-field wpi-col2 wpi-colf"> | |
<label for="wpinv_alternate_phone" class="wpi-label"><?php esc_attr_e( 'Alternate Phone', 'invoicing' ); ?><span class="wpi-required">*</span></label> | |
<span id="wpinv-wpinv_alternate_phone-wrap"> | |
<input name="wpinv_alternate_phone" id="wpinv_alternate_phone" class="wpi-input form-control" required="required" type="text" value="<?php echo $alternate_phone; ?>" /> | |
</span> | |
</p> | |
<p class="wpi-cart-field wpi-col wpi-colf wpi-coll"> | |
<label for="wpinv_company_desc" class="wpi-label"><?php esc_attr_e( 'Company short description', 'invoicing' ); ?></label> | |
<span id="wpinv-wpinv_company_desc-wrap"> | |
<input name="wpinv_company_desc" id="wpinv_company_desc" class="wpi-input form-control" type="text" value="<?php echo $company_desc; ?>" /> | |
</span> | |
</p> | |
<?php | |
} | |
add_action( 'wpinv_checkout_billing_fields_last', 'wpi_custom_display_checkout_billing_fields', 10, 1 ); | |
// Make custom field(alternate phone) mandatory field. | |
function wpi_custom_checkout_required_fields( $required_fields ) { | |
$required_fields['alternate_phone'] = array( | |
'error_id' => 'invalid_alternate_phone', | |
'error_message' => __( 'Please enter a valid alternate phone number.', 'invoicing' ) | |
); | |
return $required_fields; | |
} | |
add_filter( 'wpinv_checkout_required_fields', 'wpi_custom_checkout_required_fields' ); | |
// Set error if custom field(alternate phone) is not valid. | |
function wpi_custom_validate_checkout_fields( $valid_data, $post ) { | |
if ( empty( $post['wpinv_alternate_phone'] ) ) { | |
wpinv_set_error( 'invalid_alternate_phone', __( 'Please enter your alternate phone number.', 'invoicing' ) ); | |
return; | |
} | |
} | |
add_action( 'wpinv_checkout_error_checks', 'wpi_custom_validate_checkout_fields', 10, 2 ); | |
// Store custom fields values(alternate phone & company short description) in invoice meta. | |
function wpi_custom_store_custom_fields( $invoice, $saved ) { | |
if ( empty( $invoice ) ) { | |
return; | |
} | |
if ( isset( $_POST['wpinv_alternate_phone'] ) ) { | |
$invoice->update_meta( '_wpi_custom_alternate_phone', sanitize_text_field( $_POST['wpinv_alternate_phone'] ) ); | |
} | |
if ( isset( $_POST['wpinv_company_desc'] ) ) { | |
$invoice->update_meta( '_wpi_custom_company_desc', sanitize_text_field( $_POST['wpinv_company_desc'] ) ); | |
} | |
} | |
add_filter( 'wpinv_invoice_save', 'wpi_custom_store_custom_fields', 10, 2 ); | |
function wpi_custom_checkout_billing_user_info( $user_info, $invoice ) { | |
if ( !empty( $invoice ) ) { | |
$user_info['alternate_phone'] = $invoice->get_meta( '_wpi_custom_alternate_phone' ); | |
$user_info['company_desc'] = $invoice->get_meta( '_wpi_custom_company_desc' ); | |
} | |
return $user_info; | |
} | |
add_action( 'wpinv_checkout_billing_details', 'wpi_custom_checkout_billing_user_info', 10, 2 ); | |
// Display custom field (alternate phone) under user address. | |
function wpinv_custom_display_field_in_user_address( $invoice ) { | |
if ( !empty( $invoice ) ) { | |
$alternate_phone = $invoice->get_meta( '_wpi_custom_alternate_phone' ); | |
if ( !empty( $alternate_phone ) ) { | |
?> | |
<div class="alternate-phone"><?php echo wp_sprintf( __( 'Alternate phone: %s' ), esc_html( $alternate_phone) ); ?></div> | |
<?php | |
} | |
} | |
} | |
add_action( 'wpinv_display_to_address_bottom', 'wpinv_custom_display_field_in_user_address', 10, 1 ); | |
// Display custom field (company short description) below invoice details. | |
function wpinv_custom_display_fields_on_invoice( $invoice ) { | |
if ( !empty( $invoice ) ) { | |
$description = $invoice->get_meta( '_wpi_custom_company_desc' ); | |
if ( !empty( $description ) ) { | |
?> | |
<div class="row wpinv-company-desc-info"> | |
<strong><?php _e( 'About company:', 'invoicing' ); ?></strong> | |
<p><?php echo $description; ?></p> | |
</div> | |
<?php | |
} | |
} | |
} | |
add_action( 'wpinv_invoice_print_after_top_content', 'wpinv_custom_display_fields_on_invoice', 9, 1 ); | |
// Add custom field (alternate phone) in billing details in email that send to the user. | |
function wpinv_custom_add_field_in_email_billing_details( $invoice ) { | |
if ( !empty( $invoice ) ) { | |
$alternate_phone = $invoice->get_meta( '_wpi_custom_alternate_phone' ); | |
if ( !empty( $alternate_phone ) ) { | |
?> | |
<tr class="wpi-receipt-phone"> | |
<th class="text-left"><?php _e( 'Alternate phone', 'invoicing' ); ?></th> | |
<td><?php echo esc_html( $alternate_phone ) ;?></td> | |
</tr> | |
<?php | |
} | |
} | |
} | |
add_action( 'wpinv_email_billing_fields_last', 'wpinv_custom_add_field_in_email_billing_details', 10, 1 ); | |
// Add custom field (company short description) below billing details in email that send to the user. | |
function wpinv_custom_add_field_below_email_billing_details( $invoice ) { | |
if ( !empty( $invoice ) ) { | |
$description = $invoice->get_meta( '_wpi_custom_company_desc' ); | |
if ( !empty( $description ) ) { | |
?> | |
<div id="wpinv-email-billing"> | |
<h3 class="wpinv-address-t"><?php echo __( 'About Company', 'invoicing' ); ?></h3> | |
<p><?php echo $description; ?></p> | |
</div> | |
<?php | |
} | |
} | |
} | |
add_action( 'wpinv_email_after_billing_details', 'wpinv_custom_add_field_below_email_billing_details', 10, 1 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment