Magento 2 – How to Apply Custom Template for Field in Checkout Form

formsmagento-2.1magento2template

Currently I can redefine ui/form/field (not input!) use my theme,

but I need to set custom template for each field. I try to use code from docs with small fix.

<item name="shippingAddress" xsi:type="array">
    <item name="children" xsi:type="array">
        <!-- The name of the form the field belongs to -->
        <item name="shipping-address-fieldset" xsi:type="array">
            <item name="children" xsi:type="array">
                <!-- the field you are customizing -->
                <item name="firstname" xsi:type="array">
                    <item name="config" xsi:type="array">
                        <!-- Assigning a new template -->
                        <!--
                        <item name="elementTmpl" xsi:type="string">%Vendor_Module%/form/element/%your_template%</item>
                        -->
                        <item name="template" xsi:type="string">Vendor_Checkout/form/field</item>
                    </item>
                </item>
            </item>
        </item>
    </item>
</item>

Templates (2 for testing…):

  • /app/code/Vendor/Checkout/view/frontend/web/template/form/field.html;
  • /app/code/Vendor/Checkout/view/frontend/web/templates/form/field.html.

This is not working. Why?

UPD: Fixed path.

Best Answer

I think I know what the problem with this.

Go to Magento\Checkout\Block\Checkout\AttributeMerger line 173. You can see static value for template.

Details in the my issue on github.

Related Topic