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?

About the author

Remi Hey! My name is Rémi Corson, I'm a French WordPress Developer, I code stuff and I write from time to time ;-) Buy a coffee
Show your support

Related Posts

1 comment

  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