To add a custom field to a WooCommerce checkout you need three hooks for the data path and two for display: woocommerce_after_checkout_billing_form prints the input, woocommerce_after_checkout_validation rejects bad values, woocommerce_checkout_update_order_meta saves it, and then woocommerce_admin_order_data_after_billing_address and woocommerce_email_order_meta_fields put it on the order screen and in the emails. The popular version of that recipe does all five and still loses the value on a shop running HPOS, because it writes to post meta and trusts the request.
Below is the snippet as it usually appears, then a refactor in four steps with the reason for each change. You need WooCommerce active and somewhere to put PHP.…