WooCommerce: How to Add a Custom Checkout Field

Let’s imagine you want to add a custom checkout field (and not an additional billing or shipping field) on the WooCommerce Checkout page. For example, it might be a customer licence number – this has got nothing to do with billing and nothing to do with shipping.

Ideally, this custom field could show above the Checkout page order notes – right after the shipping form. It will feature a label, an input field, and will be required.

So, here’s how you do it – hope it helps you understand that anything is possible with WooCommerce!

Here’s our new custom checkout field, purposely displayed outside the billing/shipping forms.

PHP Snippet (Part 1 of 3): Add New Field @ WooCommerce Checkout

Here we display the new field on the WooCommerce Checkout page, and specifically above the “Order Notes”, which usually displays at the end of the shipping form.

The new field ID is “license_no“, which will be useful to remember in parts 2 and 3.

/**
 * @snippet       Add Custom Field @ WooCommerce Checkout Page
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 6
 * @community     https://businessbloomer.com/club/
 */
 
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' ) ); 
}

PHP Snippet (Part 2 of 3): Validate New Checkout Field

Now, once the checkout is processed, we want to make sure our field is not empty. Remember, we chose “required” => true which means the field will have a required mark beside its label. However, this is not enough – we still need to generate an error message if our field is empty.

/**
 * @snippet       Validate Custom Field @ WooCommerce Checkout Page
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 6
 * @community     https://businessbloomer.com/club/
 */

add_action( 'woocommerce_checkout_process', 'bbloomer_validate_new_checkout_field' );
 
function bbloomer_validate_new_checkout_field() {    
   if ( ! $_POST['license_no'] ) {
      wc_add_notice( 'Please enter your Licence Number', 'error' );
   }
}

PHP Snippet (Part 3 of 3): Save & Show New Field @ WooCommerce Thank You Page, Order Page & Emails

If validation is passed, WooCommerce processes the order. But the new field value gets lost, as there is no function that “stores” that value into the so-called “Order Meta Data”. We need to save and also to display the field value inside orders and order emails.

/**
 * @snippet       Save & Display Custom Field @ WooCommerce Order
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 6
 * @community     https://businessbloomer.com/club/
 */

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_thankyou', 'bbloomer_show_new_checkout_field_thankyou' );
  
function bbloomer_show_new_checkout_field_thankyou( $order_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_admin_order_data_after_billing_address', 'bbloomer_show_new_checkout_field_order' );
  
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>';
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Add Content to a Specific Order Email
    Customizing WooCommerce emails via the WordPress dashboard is not easy and – sometimes – not possible. For example, you can’t edit or add content to them unless you’re familiar with code. Well, here’s a quick example to learn how to add content to any WooCommerce default order email. In this case study, our goal is […]
  • WooCommerce: Cart and Checkout on the Same Page
    This is your ultimate guide – complete with shortcodes, snippets and workarounds – to completely skip the Cart page and have both cart table and checkout form on the same (Checkout) page. But first… why’d you want to do this? Well, if you sell high ticket products (i.e. on average, you sell no more than […]
  • WooCommerce: Disable Payment Method If Product Category @ Cart
    Today we take a look at the WooCommerce Checkout and specifically at how to disable a payment gateway (e.g. PayPal) if a specific product category is in the Cart. There are two tasks to code in this case: (1) based on all the products in the Cart, calculate the list of product categories in the […]
  • WooCommerce: Add Privacy Policy Checkbox @ Checkout
    Here’s a snippet regarding the checkout page. If you’ve been affected by GDPR, you will know you now need users to give you Privacy Policy consent. Or, you might need customer to acknowledge special shipping requirements for example. So, how do we display an additional tick box on the Checkout page (together with the existing […]
  • WooCommerce: Redirect to Custom Thank you Page
    How can you redirect customers to a beautifully looking, custom, thank you page? Thankfully you can add some PHP code to your functions.php or install a simple plugin and define a redirect to a custom WordPress page (as opposed to the default order-received endpoint). This is a great way for you to add specific up-sells, […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

105 thoughts on “WooCommerce: How to Add a Custom Checkout Field

  1. Hi Rodolfo
    Great tutorial!
    I have a question, is there anyway we can make this custom field editable within WooCommerce orders?

    1. Like – when you want to edit the Billing or the Shipping section in the admin order edit page?

  2. Hello, thank you very much for your input. I have already added the custom field I needed, in my case ‘VAT Number’. Additionally I have another code (also yours πŸ™‚ ) in which I remove fields from the checkout if the value in euros of the order is 0. I’m trying not to show this new field, I think the problem is in recovering the field for the unset, but I can’t, what’s wrong?

    add_filter( 'woocommerce_checkout_fields' , 'bbloomer_simplify_checkout_virtual' );
     
    function bbloomer_simplify_checkout_virtual( $fields ) {
         
        $total = 0;
         
        foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            // Check if there are some product with price over zero
            $current_price = $cart_item['data']->price;
            // error_log('Este es el precio de un item: '.$current_price);
            if ( $current_price != 0 ) $total = $total + $current_price; 
        }
         
       // error_log('Este es el precio de TOTAL: '.$total);
        if( $total == 0 ) {
            // unset($fields['billing']['billing_first_name']);
            // unset($fields['billing']['billing_last_name']);
            unset($fields['billing']['billing_company']);
            unset($fields['billing']['billing_address_1']);
            unset($fields['billing']['billing_address_2']);
            unset($fields['billing']['billing_city']);
            unset($fields['billing']['billing_postcode']);
            unset($fields['billing']['billing_country']);
            unset($fields['billing']['billing_state']);
            unset($fields['billing']['billing_phone']);
    		unset($fields['billing']['vat_number']);
            add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
        }
         
        return $fields;
    }
    
    1. Hello Iban! Does the $total variable work correctly, I mean does it change value in case there are non-free products?

  3. Great tutorial. Just wondering how you would solve the issue of using ‘0’ as a value option? My field is required but when a user selects the 0 option It won’t save due to validation saying, ‘Profit margin is a required field’. I can’t leave the value blank either as this results in a validation error. Am I best typing out a word such as ‘blank’? Here is my code:

          $fields['shipping_profit_margin'] = array(
    	  'type' => 'select',
    	 'label'     => __('Profit margin', 'woocommerce'),
    	 'required'  => true,
    	 'class'     => array('input-select'),
    	 'clear'     => true,
    	 'options' => array(
    	 			'-' => '0',
    	 			'5' => '5',
    				'10' => '10',
    	 			'15' => '15',
    	 			'20' => '20',
    	 			'25' => '25',
    				'30' => '30',
    				'35' => '35',
    	 			'40' => '40',
    	 			'45' => '45',
    				'50' => '50',
    				'55' => '55',
    	 			'60' => '60',
    	 			'65' => '65',
    				'70' => '70',
    	 			'75' => '75')
    	  );
    1. Why not use “0” as the first option key?

  4. Hi Rodolf,
    I have tried to add this code snippet in one of our client website but does not seems to be working.
    Reason I think is due to checkout.js is being used on checkout page?

    1. You mean you use the WooCommerce Checkout Block?

  5. Hi Rodolfo,

    Thank you for your insight as usual.

    I was wondering if there’s a specific reason you are not using/updating the

    woocommerce_checkout_fields

    hook like you did in another post https://www.businessbloomer.com/woocommerce-checkout-customization/ , appending to the existing

    $fields

    . Or what the differences would be.

    Thanks again!

    1. Hello Ken! This one is a non-billing, non-shipping custom field. When I use woocommerce_checkout_fields, on the other hand, I add a new billing/shipping/order field.

      1. Thanks for your reply, yes sorry i read the other posts after commenting and wasn’t able to add a reply to a unapproved comment.

        If i may ask another question, is it a bad practice adding the fields directly to the checkout page by injecting it as a template into form-checkout.php and then add the fields via hook e.g. woocommerce_checkout_fields or woocommerce_process_shop_order_meta using $_POST ?

        1. Not sure I entirely follow, but if you mean you wish to overwrite the plugin files, then that’s bad practice. Actions and filters are the only way.

  6. Looks like a typo in Part 3 where you’re declaring “bbloomer_show_new_checkout_field_order” twice. Reading it I think you mean the first one to be “bbloomer_show_new_checkout_field_thankyou”. πŸ™‚

  7. Not working anymore for showing in order info and email.

    1. Hi Diana! Order info works for sure, email I haven’t been able to test yet but I don’t see why not – nothing has changed in WooCommerce code base.

      Did you change anything in my snippet? Feel free to share your code in case.

      1. Seem like you used:
        _license_no

        but should it be:
        license_no
        in part 2 and 3

        1. Are you sure Kjeld? did you get an error maybe?

  8. Hi Rodolfo,
    Thanks for your effort.

    Doesnt work for me, sorry

    1.- Display the fields. Yes
    2.- Validate. Yes
    3.- Show in Order Form. No

    Rehub theme, php 7.4

    1. Hi Javier, thanks so much for your comment! Works for me, so I’m afraid this requires custom troubleshooting work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  9. Thanks for the great tutorial. Very useful. I was wandering if you can give some tips on adding multiple checkout fields, validating, saving and emailing them. I really appreciate your help.

    1. Hi Pasindu, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  10. As of today, it’s not working.
    If I exactly copy paste the code. It gives error of “Enter License Number”

    That means that the “license_no” field value is not being posted correctly. Can you help on that? I’m using WP 5.9

    1. Thanks for your feedback Adnan, but it works for me. Error should say “‘Please enter your Licence Number'”

  11. I have pasted that snippet in my function.php. It cause the “There has been a critical error on this website.” How to fix it.

    1. Hi Salman, maybe you forgot something when you pasted? Snippet works perfectly

  12. Hi Rodolfo,
    I have one question. Is it possible to hide added custom field when we haven’t products in order with defined shipping class ? I need set custom field only when exist in order minimum 1 product with difined shipping class (e.g. number 1).

    1. Yep, that’s possible. Try searching for “shipping class” on this site and you should find some related snippet

  13. It works fantastically! However, I would like to save the field so that users don’t have to enter it again each time, how can I do that?
    Thank you very much!

    1. Hey Luca thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  14. Hi Rodolfo, thanks for this useful article.
    Is there a way to make this field editable on the actual order screen?

    1. Hi Phil, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  15. This was a huge help, I am looking to now display the field on a front-end page. I was thinking it would work like below but I keep getting an error. thanks for the help.

    <?php
    $args = array(
       'status'          => 'completed',
    );
    				
    	$orders = wc_get_orders( $args );
    				
    foreach ( $orders as $order ) {
    	$order_id = $order->get_id();
    	$order_billing_first_name = $order->get_billing_license_no();
    
    echo '<ul>';
    	echo '<li>' . $order_id . '</li>';
    	echo '<li>' . $order_license_no . '</li>';
    	echo '</ul>';
    	}
    ?>
    
    1. I made a mistake on on my var it should read

       $order_license_no = $order->get_billing_license_no(); 

      thanks

      1. Problem is that get_billing_license_no() function does not exist. You could try with the following, edit “license_no” in case that’s not the name of your custom field:

        $order_license_no = get_post_meta( $order_id, 'license_no', true );
  16. Hi Rodolfo,

    Thank you for pointing me to this amazing snippet. Worked like a charm while adding the birthyear field to my checkout page.

  17. Good evening and congratulations on all the articles, you are very helpful. I would like to ask if you have a way to enter a second box for entering the coupon. Let me give you an example. In the checkout there must be coupon boxes for promotional discount codes, and a second coupon box for influencer discount codes. Thank you very much and good work.

    1. Hello Andrea, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  18. Hey there – great tutorial.

    What if we wanted to match a custom field ie. ensure licence number field contained CA for an if statement?
    How do we go about checking the content of this custom field and not just displaying it?

    1. Hello Ben, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  19. Thank you very much for this useful tutorial and code!
    How can I get the created field displayed on top of the page?
    Above the billing detail is that possible?

    1. Hi Sascha, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  20. HI! This is a awesome piece of code, this is that I was finding. Thank you so much.
    I just have a question. How can I do the custom field appear in the next checkout order and can edit in the “my-account” customer page.

    Thanks in advance.

    1. Thank you Edwin, this shows on the thank you page if I’m not wrong. To make it editable from My Account, yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  21. Howdy Rodolfo.

    This is a great piece of coding – thank you so much.

    Just wondering if there is a way to show this on the actual order screen in Woocommerce admin please?

    Cheers,
    Phil.

    1. Hi Phil, part 3 already does that

  22. Hi Rodolfo,

    This works really good! Only thing I’m missing is I don’t see this field back in the order details in Woocommerce. I see it in the emails, but when I check the order in the shop, it’s not showing. Is there a way to add that as well?

    1. Hi Loes, bbloomer_show_new_checkout_field_order() function does that already

      1. in order page in woocommerce still not seeing custom chckout field i added nut i saw in backend order and email

        1. Sorry Harish, works for me

  23. Hi Rodolfo,

    Thank you so much for all your great tutorials and snippets. I’m fan πŸ˜‰
    Could this be done with radio buttons and how?

    1. Hi Martin, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  24. Hi Rodolfo,

    Your snippet still works and it’s amazing. One other thing. How do I include it on the email as well?

    1. Thank you! I’ve just revised the whole tutorial, take a look

  25. Hi Rodolfo

    Thanks for this great site, you’re a legend πŸ™‚

    I appear to be having similar issues to the most recent comments: part 5, get_post_meta() appears to return an empty value. update_post_meta() works because I can see the custom field and value in the order dashboard. I just can’t seem to ‘extract’ the value and display it. Any ideas?

    1. Hi David, where do you need to display it?

      1. I want to display the custom field on the ‘thank you’ page. Interestingly, get_post_meta() works when applied to woocommerce_admin_order_data_after_billing_address but it returns nothing when applied to woocommerce_thankyou. I’m beginning to wonder if maybe the custom field is stored in the database after the ‘thank you’ page has been displayed, so the data it’s trying to access isn’t there yet. Really weird!

        1. No David, the thank you page is not too early. There must be something else πŸ™‚ Code you used?

  26. Hello Sir Rodolfo, thank you very much for valuable tutorial. I followed the syntax on part 5 of 5, but I think update_post_meta doesn’t save to database, the custom field returns blank data on order/thank you page, please help.

    1. Did you change the code? If yes paste it here and I’ll see if I can spot the error

  27. Hi, thanks for the information. I follow the instructions, but the new fields are shown blank on the preview popup in the order page. You have to enter into the order details to read these new fields.

    1. Hello Greg, did you edit my code? I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  28. Hey, I’m trying to add custom checkout fields if a product attribute be in the cart. I wrote the code based on shipping method, but couldn’t find anything for making it work with product attribute change. can you help me please. is it possible or not? I need it a lot. thanks πŸ™‚

    1. Hello Somy, thanks so much for your comment! Yes, this is possible – unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

  29. I tried your code and it worked well. However, the field is marked as required (with a red star next to it, but the checkout form gets submitted even when it’s blank. Any clue why?

    Thanks so much. I really enjoy your site. It’s so helpful.

    1. Hey Rocheli, thanks for your comment! You’re missing another part of the snippet where validation is done… check my tutorial again πŸ™‚

  30. Very nice!
    There is just one thing that upsets me in the last step 5 out of 5.
    Isn’t there an alternative way to add it directly to the arrays of fields and not inserting HTML?
    Possibly making it also editable admin side for manual order updates.

    1. Hey Rayduxs, thanks so much for your comment! Yes, this is possible – unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  31. Hello Rodolfo,
    Thank you so much again amazing tutorial. I just searching custom conditional field by user role at checkout page. It seems so simple but i could not handle it. I have a custom role which same as customer (calling WHOLESALER) but this role just having COMPANY.
    So i want to make before checkout billing field a radio field with two options ” is personal or company”
    When customer check personal then display default checkout’s fields except COMPANY NAME.
    When customer check company then display default fields + Company Tax Number and Company Tax Name.
    And the fields (company tax name and tax number and company name ) should visible WHOLESALER’s account’s page ( it should not be editeble)
    And the fields should automatically filling if WHOLESALER sign in their login information.
    Could you please help me for this or directive how can do that?

    1. Hello Ahmet, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

  32. Hello Rodolfo. Thank you very much for your tutorials. I’ve used several!

    About this in particular: How could I turn the field into a dropdown (that needs to be saved and retrieved multiple times)

    Very obliged if you can help!

    1. Hey Marcio, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  33. In Part 5 of 5, you reused the function name from updating user_meta to update post_meta.

    add_action('woocommerce_checkout_update_order_meta', 'bbloomer_custom_checkout_field_update_user_meta');

    This conflicts with the code from Part 3 of 5.

    It think this fixes the issue:

    // Update order meta with field value
     
    add_action('woocommerce_checkout_update_order_meta', 'bbloomer_custom_checkout_field_update_order_meta');
     
    function bbloomer_custom_checkout_field_update_order_meta( $order_id ) { 
    if ($_POST['student_acu_no']) update_post_meta( $order_id, '_student_acu_no', sanitize_text_field($_POST['student_acu_no']) );
    }
     
    // Display User Field @ Order Meta
     
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'bbloomer_checkout_field_display_admin_order_meta', 10, 1 );
     
    function bbloomer_checkout_field_display_admin_order_meta($order){    
    echo '<p><strong>'.__('Acu #').':</strong> ' . get_post_meta( $order->id, '_student_acu_no', true ) . '</p>';
    }
    
    1. Hey JB, thanks so much for that. Sure there was a conflict?

  34. Hi Rodolfo,

    Thanks for all the tutorials you have provided for free.

    Can you please tell me how to display a custom field created for checkout page on any other page.

    e.g. I want to display a custom field in my invoice.

    1. Hey Omkar, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  35. Please how do I override the default :
    do_action( β€˜woocommerce_review_order_after_order_total’ );

    in the checkout page. Also where is the location of the default implementation.

    Thanks in advance for your support

    1. Hello Webb, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  36. Hi, Rodolfo!

    Thank you so much for this guide.
    Is there a way to change order of this fields?

    I would like to make a field ‘Company ID number’ (this information is required in Thailand), and place it after ‘Company name’ field.

    Currenly for re-orderind standart fields using this snippet:

    function bbloomer_move_checkout_fields_woo_3( $fields ) {
      $fields['first_name']['priority'] = 95;
      $fields['last_name']['priority'] = 96;
        // 100 110 phone & e-mail
      $fields['company']['priority'] = 130;
      $fields['country']['priority'] = 140;    
      $fields['address_1']['priority'] = 150;
      $fields['address_2']['priority'] = 160;
      $fields['city']['priority'] = 170;
      $fields['state']['priority'] = 180;
      $fields['postcode']['priority'] = 190;
        return $fields;
    }
    

    Woo version 3.0 +

    Thank you!

    1. Tim, thanks for this – I see you’re referencing my snippet from https://businessbloomer.com/woocommerce-move-reorder-fields-checkout-page/. What have you tried so far with your custom field?

    2. Rodolfo, exactly! For ordering I am also using your snippet.

      I’ve tried

      $fields['custom_field']['priority'] = 101;
      

      But that method isn’t working for custom field

      1. Uhm, I see, sorry to hear that. Unfortunately I can’t spend too much time on custom support here in the blog πŸ™‚

    3. What action or filter to we hook this to?

  37. Hi,
    Great site and tutorials!
    If I only want to have a field so my customer could sign there personal number, and it must be optional.
    How do I do that?

    ThankΒ΄s

    1. IΒ΄m sorry!
      I ment it must be mandatory. not optional.

      1. Hey Greger! The line:

        'required'   => true,
        

        is already telling Woo to make the field mandatory πŸ™‚

  38. hi, Rodolfo.
    First of all – thank you for this amazing site. It has so much useful information, it’s unbelievable.
    Now, could i use this code to only let the admin put in a number? if so, how?
    I would need a field where i as admin could enter a specific document number to that field and it should be visible in order and email, but only for one order. On the next order of any client i would then put another number in.
    And is there a way to change the price on checkout by admin too?

    Thank you for everything you do so we who are just starting can have it so much easier.

    1. Hey Mark, thanks so much for your comment! You should use https://codex.wordpress.org/Function_Reference/is_admin to conditionally show the field. Also, changing the price at checkout is possible, but very custom and unfortunately I can’t offer a complementary fix here on the blog. Thanks for your understanding πŸ™‚

  39. Great tutorial, thank you Rodolfo.

    I have followed it closely and tested after adding each snippet, and it’s working perfectly.

    Except, the last snippet. The field name shows on the order page, eg: Acu#, but the value doesn’t show.

    I have checked my code against yours and in the woocommerce docs.

    Any idea why this won’t work?

    Thank you

    1. Hey Jason, thanks for your comment! The only thing I can think of without testing is the “_student_acu_no” bit in snippet #5. Try to remove the initial underscore “_” from there and see if it works πŸ™‚ Let me know!

      1. Hi Rodolfo, I did try that and it didn’t work.

        I also tried update_post_meta, like in snippet 3, to save the data to the post meta database. Still no luck.

        1. Uhm, weird. Luckily, today I’m working with a client on this exact same task, so I will let you know if it works (and if not, remind me in a couple of days). R

    2. To anyone else having this problem. You need to update you need to change the “esc_attr” in “esc_attr($_POST[‘student_acu_no’])” with “sanitize_text_field”. So it becomes “sanitize_text_field($_POST[‘student_acu_no’])”

      This worked for me to get it to show up in backend.

      Btw, great article Rodolfo. I love your site.

  40. Hey Rodolfo,

    Great site and tutorials! I have a site using woocommerce that needs something very similar.. was hoping you might point me in the right direction.. I need to add a select list to checkout, for an employee to choose their regional director from.. the confirmation email has to be sent to the regional director so they can login and change the order status.. showing their approval or denial of the order.

    What is the best way to:
    1) Add a select list for a customer to choose a value from (in this case their Regional Director, whose email would be the value we need)
    2) Add that email to the admin order confirmation email

    Thank you for any help you can provide! Also, are you available for small projects like this one I just asked about above? I’m a UX Engineer and have a blast creating engaging sites, and hand coding the front end, some backend, etc. But, it would be great to have someone with your skill set for these tasks that are out of my area of expertise! Let me know and thanks again.

    1. Jesse, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here on the blog.

      Indeed, I provide WooCommerce support and customization on an hourly basis. If you’d like to get a quote, feel free to contact me here.

      Thanks a lot for your understanding!

      ~R

  41. Hi Rodolfo, I would like to automate the address fields in this way,

    When we select a ‘country’, it will activate the ‘province’ field with select option below the country field.
    Now when we select a ‘province’, it will activate another field ‘city’ with the same select option below the ‘province’ field.
    And finally when we select a city, it will show the ‘address’ field where we can write the house/street no etc.

    I’ve googled but yet couldn’t find a solution for this anywhere. I hope soon I will hear from you. Thanks in advance πŸ™‚

    1. Hello Mahir, thanks for your comment! And nice one by the way – unfortunately this needs to be custom coded and I can’t offer a free solution here on the blog. I would recommend, however, to check this tutorial to reorder fields on the checkout: https://businessbloomer.com/woocommerce-move-reorder-fields-checkout-page/, and then after that you’d need to add 2 new fields (city & province dropdowns). You can follow in this case this tutorial that does a similar thing (not a dropdown though): https://businessbloomer.com/woocommerce-add-house-number-field-checkout/. Hope this helps πŸ™‚

  42. hi! is it possible to make it visible in multiple categories (online-courses, books)?

    1. Hey Filippo, thanks for your comment! Yes, you can just add another function like this:

      
      function bbloomer_is_category_19_in_cart( $product_id ){
      // ID 19 = other cat
      return has_term( 19, 'product_cat', get_post( $product_id ) );
      }
      
      

      And then add another check in the existing function:

      
      if( bbloomer_is_category_18_in_cart( $product_id ) || bbloomer_is_category_19_in_cart( $product_id ) ){
      
      

      Let me know πŸ™‚

  43. Thank you very much for this useful tutorial and code.

    However, If I want to add options to the address field instead of text box, how can that be done.

    for example I only ship to three areas in one city and I want them to be able to choose from these areas under the Address, instead of entering their address manually.

    If you have an existing tutorial, i would appreciate your guidance to it.

    Thank you

    1. Great question Sara, thanks! You’re basically looking for a way to limit shipping to certain “default” areas e.g. “New York City 10001” or “New York City 10009”, aren’t you? If yes, does every area have a specific street address, zip code, city, state and country?

  44. Great Rodolfo,

    Nice, flexible options will try definitely

    1. Thank you so much Lubo!

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *