Magento 2 – How to Add New Custom Field in Contact Form

contact-formcontact-usformsmagento2

Could you please help to add a new custom field in Magento 2 contact form.

Best Answer

Open your form.phtml file.

/magento_root/app/design/frontend/VENDER/YOUR_THEME/Magento_Contact/templates/form.phtml

Then add mycustomfield field to this contact form:

<div class="field mycustomfield required">
    <label class="label" for="mycustomfield"><span><?php /* @escapeNotVerified */ echo __('my custom field') ?></span></label>
    <div class="control">
        <input name="mycustomfield" id="mycustomfield" title="<?php /* @escapeNotVerified */ echo __('my custom field') ?>" value="" class="input-text" type="text" data-validate="{required:true}"/>
    </div>
</div>

Add the above field in the form.

Go to MARKETING > Communication > Email Templates in the Magento Admin section. Click "Add New Template" and from the "Template" dropdown box select "Contact Form" then "Load Template". Under template content you can add your custom field:

Add your new field as per your requirement. it should looks like this:

{{trans "My Custom Field: %mycustomfield" mycustomfield=$data.mycustomfield}}
{{trans "Name: %name" name=$data.name}}
{{trans "Email: %email" email=$data.email}}
{{trans "Phone Number: %telephone" telephone=$data.telephone}}
{{trans "Comment: %comment" comment=$data.comment}}

Enter a new name under "Template Name" to save your new Template and click on "Save Template". Now we need to tell Magento to use this new template for the Contact form.

Go to STORES -> Settings -> Configuration -> General -> Contacts and select "Contacts". Under "Email Options",

Select your new template under the "Email Options" -> "Email Template" dropdown box.

Click on "Save Config".

Related Topic