less than a minute read • Updated 2 hours ago
Pre-populate custom checkout fields
How to prepopulate a custom checkout field's value using a hidden session attribute and Twig.
Unlike Foxy’s standard checkout fields, custom fields you create aren’t prepopulated automatically. You can add that behavior yourself using Twig, pulling the value from a hidden session attribute passed in through the cart.
Steps
{% if Referred_By is not defined %}
{% set Referred_By = "" %}
{% for key, custom_field in custom_fields %}
{% if key == "Referred_By" %}
{% set Referred_By = custom_field.value %}
{% endif %}
{% endfor %}
{% endif %}
<input type="text" id="Referred_By" name="Referred_By" class="fc-form-control" aria-required="true" value="{{ Referred_By }}" data-fc-required>
Notes
This is a separate mechanism from keeping a field’s value in place when the checkout re-renders after the customer has already typed something in — see Keep custom field values when the checkout re-renders for that.