Customise WooCommerce Checkout Fields Based on Products in Cart

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?

One response to “Customise WooCommerce Checkout Fields Based on Products in Cart”

  1. visualizedigital

    Thanks for the code snippet. Do you have another function to add the new fields to the order details page if the order included the relative products?

Leave a Reply