Conditional Fields for Variable Products
The ability to have conditional fields based on a product’s variation is something that has been requested by a lot of customers over the years. It is now possible to display fields conditionally based on the product variation that is in the customer’s cart.
How Does This Work?
First, you need to have a product that has one or more variations. For example, if the product is an event registration, you might have full day and half day options as the product’s variations.
For this event, you’ll provide lunch to the full day attendees, but not half day attendees. It makes sense that you need to know the full day attendees’ meal choices. However, it wouldn’t make much sense to ask half day attendees what their meal choice is.
So in this case we only want to ask customers who purchase the “Full Day” variation their meal choice.
Add A Conditional Checkout Field In WooCommerce For A Specific Product Variation
You’ll need to create a field for the meal choices and set it to display for customers purchasing the full day option.
First, navigate to WooCommerce > Checkout Fields > Conditional Fields tab, and click the Add New Field button at the top.
Many of the settings for this field will depend on the type of information you are collecting. We’ll touch on some of the options you might want to use.
- Title: Make this specific to what information you are collecting from your customer. Something like “Full Day Event Meal Choice” would work just fine.
- Required Field: If you must have this information before a customer checks out, check this box.
- Required Field Error Message: Enter some descriptive text to let your customers know that this field is required.
- Repeat Field: If your customers can purchase more than one ticket to this event, it might make sense to have the field repeat (i.e. show up once for whatever quantity they are purchasing).
- Minimum Quantity: You may not need a customer to complete a certain field if they are only buying one ticket. In this case you can enter 2 and the field will only be displayed if the customer is buying two or more. An example of this would be if you are already collecting the information in the shipping/billing fields such as a name. You can probably assume that the customer's name would be the same thing they would enter in the conditional field. If you want the conditional field to be displayed for all quantities, leave this setting blank.
- Products: Select the product you created for this event.
- Product Variations: Enter a comma separated list of variation IDs and/or attribute slugs (the URL friendly version of the attribute name) that should trigger this field. For this example, we’ll use variation IDs. If your product has variation IDs 1, 2, 3 and you want the field to show for variations 2, and 3 you would enter 2,3. In this example, we only want the Full Day variation so we’ll enter 12364 from the screenshot above.
- Input Type: This will vary based on the type of information you’re trying to collect. In this example we want to collect meal choices, which are likely limited so we’ll use a Select Menu. Then we’ll enter the meal choices in the Select Options field.
Display Conditional Fields For Product Variations In WooCommerce
Now, a customer will see the conditional field only if they purchase the full day event ticket.
On the other hand, if they purchase the half day option, they won’t see the meal choice field.
Specifying Product Variations
There are two ways to specify product variations.
- The variation ID as shown in the screenshots above.
- The attribute’s slug.
You can use either method, or both at the same time – the choice is yours!
To get the attribute’s slug, navigate to Products > Attributes > Click Configure Terms under the variation terms. Click Quick Edit under each variation that you want to get the slug for, and copy it into the Product Variation settings for your field.
The benefit to using the attribute’s slug, is it doesn’t matter if you have the product specified in your field’s settings. So using the event example from before, let’s say you put on a lot of events all the time. Instead of adding each new event’s “product” to your field’s settings, you can simply set the field to look for the variation full-day. Leave the Product field empty, so the plugin will know to look for any product with the full-day variation.
On the other hand, if you entered a Product in the field, the plugin will only display the field if the product(s) you specify with the variation(s) you specify are in the customer’s cart.