Last active
September 7, 2022 16:12
-
-
Save zakirsajib/a82f4e2f092cfad0eb00da6dcc9fb08c to your computer and use it in GitHub Desktop.
Adding User Details before billing details in checkout page in Woocommerce
This file contains hidden or 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
add_action( 'woocommerce_checkout_before_customer_details', 'superdville_checkout_fields_before_billing_details', 20 ); | |
function superdville_checkout_fields_before_billing_details(){ | |
$domain = 'superDville'; | |
$checkout = WC()->checkout; | |
echo '<div class="col2-set" id="superdville_custom_checkout_field">'; | |
echo '<div class="col-1">'; | |
echo '<h3>' . __('User Details') . '</h3>'; | |
woocommerce_form_field( 'your_role', array( | |
'type' => 'select', | |
'label' => __('Your role', $domain ), | |
'class' => array('my-field-class form-row-wide'), | |
'required' => true, | |
'options' => array( | |
'' => __( 'Select your role', $domain ), | |
'Special Ed Tutor' => __( 'Special Ed Tutor', $domain ), | |
'Homeschooler' => __( 'Homeschooler', $domain ), | |
'Counselor' => __( 'Counselor', $domain ), | |
'Parent' => __( 'Parent', $domain ), | |
'Other' => __( 'Other', $domain ) | |
) | |
), $checkout->get_value( 'your_role' ) ); | |
woocommerce_form_field( 'your_role_other', array( | |
'type' => 'text', | |
'class' => array('my-field-class form-row-wide otherrole'), | |
'placeholder' => __('Enter your role'), | |
'default' => __(''), | |
), $checkout->get_value( 'your_role_other' ) ); | |
woocommerce_form_field( 'grade_students', array( | |
'type' => 'select', | |
'label' => __('Average grade of students', $domain ), | |
'class' => array('my-field-class form-row-wide'), | |
'required' => true, | |
'options' => array( | |
'' => __( 'Select average grade of students', $domain ), | |
'Pre-K' => __( 'Pre-K', $domain ), | |
'Pre-K' => __( 'K-2nd', $domain ), | |
'3rd-5th' => __( '3rd-5th', $domain ), | |
'Middle School' => __( 'Middle School', $domain ), | |
'High School' => __( 'High School', $domain ) | |
) | |
), $checkout->get_value( 'grade_students' ) ); | |
woocommerce_form_field( 'number_kids', array( | |
'type' => 'select', | |
'label' => __('The number of kids you work with', $domain ), | |
'class' => array('my-field-class form-row-wide'), | |
'required' => true, | |
'options' => array( | |
'' => __( 'Select number of kids you work with', $domain ), | |
'Less than 5' => __( 'Less than 5', $domain ), | |
'More than 5' => __( 'More than 5', $domain ), | |
'Other' => __( 'Other', $domain ) | |
) | |
), $checkout->get_value( 'number_kids' ) ); | |
woocommerce_form_field( 'other_number_kids', array( | |
'type' => 'text', | |
'class' => array('my-field-class form-row-wide other'), | |
'placeholder' => __('Enter the number of kids you work with'), | |
'default' => __(''), | |
), $checkout->get_value( 'other_number_kids' ) ); | |
echo '</div>'; | |
echo '</div>'; | |
} | |
// Custom checkout fields validation | |
add_action( 'woocommerce_checkout_process', 'superdville_checkout_field_process' ); | |
function superdville_checkout_field_process() { | |
if ( isset($_POST['your_role']) && empty($_POST['your_role']) ) | |
wc_add_notice( __( 'Please fill in "Your role field".' ), 'error' ); | |
if( ($_POST['your_role'] == 'Other' ) && empty($_POST['your_role_other']) ): | |
//if ( isset($_POST['your_role_other']) && empty($_POST['your_role_other']) ) | |
wc_add_notice( __( 'Please fill in "Your other role field".' ), 'error' ); | |
endif; | |
if ( isset($_POST['grade_students']) && empty($_POST['grade_students']) ) | |
wc_add_notice( __( 'Please fill in "average grade of students field".' ), 'error' ); | |
if ( isset($_POST['number_kids']) && empty($_POST['number_kids']) ) | |
wc_add_notice( __( 'Please fill in "the number of kids you work with field".' ), 'error' ); | |
if( ($_POST['number_kids'] == 'Other' ) && empty($_POST['other_number_kids']) ): | |
//if ( isset($_POST['other_number_kids']) && empty($_POST['other_number_kids']) ) | |
wc_add_notice( __( 'Please fill in "the number of kids you work with field".' ), 'error' ); | |
endif; | |
} | |
//* Update the order meta with field value | |
add_action('woocommerce_checkout_update_order_meta', 'superdville_select_checkout_field_update_order_meta'); | |
function superdville_select_checkout_field_update_order_meta( $order_id ) { | |
if ($_POST['your_role']) update_post_meta( $order_id, 'your_role', esc_attr($_POST['your_role'])); | |
if ($_POST['your_role_other']) update_post_meta( $order_id, 'your_role_other', esc_attr($_POST['your_role_other'])); | |
if ($_POST['grade_students']) update_post_meta( $order_id, 'grade_students', esc_attr($_POST['grade_students'])); | |
if ($_POST['number_kids']) update_post_meta( $order_id, 'number_kids', esc_attr($_POST['number_kids'])); | |
if ($_POST['other_number_kids']) update_post_meta( $order_id, 'other_number_kids', esc_attr($_POST['other_number_kids'])); | |
} | |
//* Display field value on the order edition page | |
add_action( 'woocommerce_admin_order_data_after_billing_address', 'superdville_select_checkout_field_display_admin_order_meta', 10, 1 ); | |
function superdville_select_checkout_field_display_admin_order_meta($order){ | |
echo '<h3>User Details</h3>'; | |
$your_role = get_post_meta( $order->id, 'your_role', true ); | |
if($your_role == 'Other'): | |
echo '<p><strong>'.__('Your role').':</strong> ' . get_post_meta( $order->id, 'your_role_other', true ) . '</p>'; | |
else: | |
echo '<p><strong>'.__('Your role').':</strong> ' . get_post_meta( $order->id, 'your_role', true ) . '</p>'; | |
endif; | |
echo '<p><strong>'.__('Average grade of students').':</strong> ' . get_post_meta( $order->id, 'grade_students', true ) . '</p>'; | |
$number_kids = get_post_meta( $order->id, 'number_kids', true ); | |
if($number_kids == 'Other'): | |
echo '<p><strong>'.__('The number of kids you work with').':</strong> ' . get_post_meta( $order->id, 'other_number_kids', true ) . '</p>'; | |
else: | |
echo '<p><strong>'.__('The number of kids you work with').':</strong> ' . get_post_meta( $order->id, 'number_kids', true ) . '</p>'; | |
endif; | |
} | |
// Add selection field value to emails | |
add_filter('woocommerce_email_order_meta_keys', 'superdville_select_order_meta_keys'); | |
function superdville_select_order_meta_keys( $keys ) { | |
echo '<h3>User Details</h3>'; | |
$keys['Your Role:'] = 'your_role'; | |
$keys['Average grade of students:'] = 'grade_students'; | |
$keys['The number of kids you work with:'] = 'number_kids'; | |
return $keys; | |
} | |
function create_custom_post_after_order( $order_id ) { | |
$order = wc_get_order( $order_id ); | |
echo '<section class="woocommerce-user-details">'; | |
echo '<h2>User Details</h2>'; | |
echo '<div class="user-details">'; | |
$your_role = get_post_meta( $order->id, 'your_role', true ); | |
if($your_role == 'Other'): | |
echo '<p><strong>'.__('Your role').':</strong> ' . get_post_meta( $order->id, 'your_role_other', true ) . '</p>'; | |
else: | |
echo '<p><strong>'.__('Your role').':</strong> ' . get_post_meta( $order->id, 'your_role', true ) . '</p>'; | |
endif; | |
echo '<p><strong>'.__('Average grade of students').':</strong> ' . get_post_meta( $order->id, 'grade_students', true ) . '</p>'; | |
$number_kids = get_post_meta( $order->id, 'number_kids', true ); | |
if($number_kids == 'Other'): | |
echo '<p><strong>'.__('The number of kids you work with').':</strong> ' . get_post_meta( $order->id, 'other_number_kids', true ) . '</p>'; | |
else: | |
echo '<p><strong>'.__('The number of kids you work with').':</strong> ' . get_post_meta( $order->id, 'number_kids', true ) . '</p>'; | |
endif; | |
echo '</div>'; | |
// Adding link to home page in thank you page | |
echo '<a href="/" class="color-neutral continue-link" style="font-size: 2.2rem;">Go back to Home Page</a>'; | |
echo '</section>'; | |
} | |
add_action( 'woocommerce_thankyou', 'create_custom_post_after_order'); | |
// disable additional fields | |
add_filter( 'woocommerce_enable_order_notes_field', '__return_false' ); | |
// Disable cart added message | |
add_filter( 'wc_add_to_cart_message_html', '__return_false' ); | |
// Remove coupon form in checkout | |
remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment