Skip to content

Instantly share code, notes, and snippets.

@AlkarE
Created January 31, 2020 18:33
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 AlkarE/32a1d9933c6d4ef3446f023fcf3fc320 to your computer and use it in GitHub Desktop.
Save AlkarE/32a1d9933c6d4ef3446f023fcf3fc320 to your computer and use it in GitHub Desktop.
add_action( 'woocommerce_before_order_notes', 'bbloomer_add_custom_checkout_field' );
function bbloomer_add_custom_checkout_field( $checkout ) {
$current_user = wp_get_current_user();
$saved_license_no = $current_user->license_no;
woocommerce_form_field( 'license_no', array(
'type' => 'text',
'class' => array( 'form-row-wide' ),
'label' => 'License Number',
'placeholder' => 'CA12345678',
'required' => true,
'default' => $saved_license_no,
), $checkout->get_value( 'license_no' ) );
}
// validation
add_action( 'woocommerce_before_order_notes', 'bbloomer_add_custom_checkout_field' );
function bbloomer_add_custom_checkout_field( $checkout ) {
$current_user = wp_get_current_user();
$saved_license_no = $current_user->license_no;
woocommerce_form_field( 'license_no', array(
'type' => 'text',
'class' => array( 'form-row-wide' ),
'label' => 'License Number',
'placeholder' => 'CA12345678',
'required' => true,
'default' => $saved_license_no,
), $checkout->get_value( 'license_no' ) );
}
add_action( 'woocommerce_checkout_update_order_meta', 'bbloomer_save_new_checkout_field' );
function bbloomer_save_new_checkout_field( $order_id ) {
if ( $_POST['license_no'] ) update_post_meta( $order_id, '_license_no', esc_attr( $_POST['license_no'] ) );
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'bbloomer_show_new_checkout_field_order', 10, 1 );
function bbloomer_show_new_checkout_field_order( $order ) {
$order_id = $order->get_id();
if ( get_post_meta( $order_id, '_license_no', true ) ) echo '<p><strong>License Number:</strong> ' . get_post_meta( $order_id, '_license_no', true ) . '</p>';
}
add_action( 'woocommerce_email_after_order_table', 'bbloomer_show_new_checkout_field_emails', 20, 4 );
function bbloomer_show_new_checkout_field_emails( $order, $sent_to_admin, $plain_text, $email ) {
if ( get_post_meta( $order->get_id(), '_license_no', true ) ) echo '<p><strong>License Number:</strong> ' . get_post_meta( $order->get_id(), '_license_no', true ) . '</p>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment