
Bill Cat relaxing once he figured out how easy it is to customize WooCommerce.
WooCommerce is used everywhere. It is so customizable and so easy to set up and maintain — plus there are TONS of add-ons for it. If you can think of it, it is probably out there.
Sometimes, though, you just want to be able to make some edits and not have to pay for an add-on. Right?
So, watch and learn. Here’s what we are going to cover:
1. Automatically setting the order status to completed
2. Adding customer checkout fields
3. Conditional logic to see if an item was purchased
Automatically Setting the Order Status to Completed
This one you may not want to do (based on your own organization business processes), but if payment was received OK and user was taken to the thank you screen, then let’s just mark the order as completed. One less thing to do!
Add this action to functions.php:
Now you can fire any kind of actions when you’ve guaranteed that the order was submitted, payment made and the thank you notification is on the screen and sent to the customer.
Adding Customer Checkout Fields
This one is a four-step process, but once you understand it, WOW. It opens up really awesome things you can do with the cart details.
All of this is done in functions.php.
Step 1: Add the field heading and HTML stuff.
This part took a minute to understand, but now I get the combination of the field “name” and WooCommerce’s CSS classes (and now you will too).
This part of the class tag is telling the WooCommerce CSS that this is a required field: validate-required.
This is the field “name” and the label (VERY IMPORTANT IN NEXT STEP): __(‘Please explain how your participation relates to the goal of the event.’).
This is making the little red star appear showing the field is required:
Step 2: Creating the field so it will show on the checkout page.
The name of the field is the first argument of the woocommerce_form_field function. In the example below, it’s called “display_theme.” It’ll also be in the $checkout->get_value function. Like this: $checkout->get_value( ‘display_theme’ ).
The type of the field is the “type” element of the array you pass to the woocommerce_form_field function. Here’s some examples!
Now putting it all together!
Step 3: Perform validation on the custom fields to ensure they are filled in. If not, show an error to the user.
The action to use for the field validation is woocommerce_checkout_process. Then just make your own function, like so:
Step 4: Write the field data to the database.
The action to use for writing back to the database is woocommerce_checkout_update_order_meta. Then just make your own function, like so:
In the update_post_meta() function, the first argument is the $order_id variable, the second is that big, long field name (including punctuation!!!) and the third is the field name when we created the field in Step 2. Slightly confusing.
One IMPORTANT thing: you MUST MUST MUST use the sanitize_text_field() function around any text inputs. This is so someone can’t put in characters or code to blow up your database or website.
And PRESTO-O CHANGE-O, here’s our order with the custom fields!

Conditional Logic to See if an Item was Purchased
You can search the cart once the order has completed to see if something specific was purchased. Or maybe you want to do something if a category of items was purchased. This is useful for making the user or customer fill out fields if they have a certain product in the cart. If they don’t, the fields don’t even show!
In the example below, I’m looking for anything in the cart that has a category of “Earth Day”:
Then I just set a variable called “found” to true. I’m using this variable to check if it is true in showing the fields, validating the fields and writing the field date back to the database.
Final Thoughts
Just like anything else, it took A LOT of trial-and-error to figure this out! WooCommerce is so powerful and so customizable, it’s really impressive. I know there are plugins out there to do this, but sometimes you just want to figure it out yourself AND keep it simple.
Now you don’t have to bash your head into the wall figuring it out, I did it all for you (and have the dents to prove it)!
