You can easily add custom fields to your checkout page in WooCommerce using custom code, or via the Checkout Field Editor add-on, but none of those methods allow you to display those fields conditionally, based on products in the cart. Here’s how to do it.
The idea here is to get the cart content and load checkout fields based on what’s in the cart. The get_cart() method allows us to retrieve the cart content, and then all you have to do is to create conditions. I am using here a simple if statement, but you can create a more robust system if needed. In my case, I am checking if products having the ids 2009 or 2010 are in the cart. If so, a new field is added for each product. Simple enough right?