1

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

  • I hope my answer will solve your query. – Nikunj Vadariya Jun 27 '18 at 9:25
2

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".

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.