Hooking into WooCommerce is very powerful when you need a customized shopping experience. There are a multitude of plugins and extensions and code snippets you can find to do almost anything you want. I had a unique situation recently that I believe will help others solve a problem. That problem is when you need a custom field on your check out form, but you only need it depending on the situation.
The company I work for uses WooCommerce Deposits to allow users an optional payment plan of four payments instead of buying the $400-$800 devices in one payment. Deposits does not allow saved credit cards or scheduling future payments with your gateway provider. Personally, I’m thinking of switching to Subscriptions so we do have that power but that’s another story.
We have an issue with people defaulting on their remaining payments. These get sent to collection agencies. Collection agencies need a social security number to really go after someone. So we decided we need to collect this information on checkout IF the user is using a payment plan.
Adding custom fields is easy. There’s a straight forward example over on Woo’s site here. I followed the Custom Special Field tutorial.
Simply we use the woocommerce_after_order_notes hook to add the field after Order Notes. Then we hook into woocommerce_checkout_process to sanitize and verify the data.
All you have to do is add a check inside these functions. WooCommerce Deposits has a function called has_deposit() that checks whether an order has a deposit or not. I copied this function and use it as my check as you can see here:
After building and testing this we realized there’s no way for us to verify this is a true SSN so we are just going to end up using a credit check API. But it’s useful functionality for how you might want to add a field depending on some other factor.