1

I need to be able to assign and output my own custom labels for the fields that are in the shipping/billing addresses during checkout in Craft Commerce.

I've already done so on the form like so:

{% set countries = craft.commerce.countriesList %}
{% set states = craft.commerce.statesArray %}
{% set addressFields = {
    firstName: {
        label: 'First Name',
        required: 1,
    },
    lastName: {
        label: 'Last Name',
        required: 1,
    },
    address1: {
        label: 'Address',
        required: 1,
    },
    address2: {
        label: 'Address 2',
        required: 1,
    },
    city: {
        label: 'City',
        required: 1,
    },
    zipCode: {
        label: 'Zip Code',
        required: 1,
    },
    phone: {
        label: 'Phone',
        required: 1,
    },
    alternativePhone: {
        label: 'Alternative Phone',
        required: 1,
    },
} %}


{% set modelName = modelName is defined ? modelName : 'address' %}
{% set model = address is defined ? address : null %}

<div class="addressBox {{ modelName }}">
    {% for key,options in addressFields %}
        <div class="form-group col-md-6 {% if model and model.getErrors(key) %}has-error{% endif %}">
            <label for="{{ modelName }}-{{ key }}">{{ options.label|t }}{{ options.required ? '*' : '' }}</label>
            <input type="text" id="{{ modelName }}-{{ key }}" class="u-full-width" name="{{ modelName }}[{{ key }}]"
                   value="{{ model ? model[key] : '' }}">
            {% if model and model.getErrors(key) %}
                <span class="flash alert-warning">{{ model.getErrors(key)|join }}</span>
            {% endif %}
        </div>
    {% endfor %}

That works like a charm. I need to however also be able to output it with my custom labels as well. Here is my currently template area, i'm not sure how i can get my custom labels in here. Please advise:

{% for address in addresses %}
     {% for key,value in address.attributes() %}

          {% switch key %}

               {% case "id" %} {# ingore #}
               {% case "customerId" %} {# ingore #}

               {% default %}

               {% if value %}
                    <li>
                         <h3 class="cart-product-details">{{ key|title }}:
                              <span class="address-option" data-key="{{ key }}" data-value="{{ value }}">
                                   {{ key == 'countryId' ? address.countryText : (key == 'stateId' ? address.stateText : value) }}
                              </span>
                        </h3>
                    </li>
               {% endif %}

          {% endswitch %}

     {% endfor %}
2

I think you're looking for the attribute function:

{% set field = attribute(addressFields, key) %}
{{ field.label }}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.