Customise WooCommerce Checkout Fields Based on Products in Cart

Remi | July 13, 2018 | in Tutorials

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?

The author

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 ;-)

Leave a reply