I'm using magento 2 and I would like to add a custom field to the contact form , how I would be able to do that ?
Magento – Add custom field to contact form Magento 2
contact-formcustomformsmagento2
Related Solutions
- Open
form.phtml
file located at app/design/frontend/default/YOUR_THEME/template/contacts folder. - Then find the following lines of code:
**
<li>
<label for="telephone"><?php echo Mage::helper('contacts')->__('Telephone') ?></label>
<div class="input-box">
<input name="telephone" id="telephone" title="<?php echo Mage::helper('contacts')->__('Telephone') ?>" value="" class="input-text" type="text" />
</div>
</li>
**
and replace it with:
<li>
<label for="subject"><?php echo Mage::helper('contacts')->__('Subject') ?> <span class="required">*</span></label>
<div class="input-box">
<input name="subject" id="subject" title="<?php echo Mage::helper('contacts')->__('Subject') ?>" value="" class="required-entry input-text" type="text"/>
</div>
</li>
- Now to show this field in contact us mail login to Admin Panel => System => Transactional Emails and click Add New Template and from the templates drop-down box select Contact Form. Then click on Load Template button.
- Under template content, find:
Telephone: {{var data.telephone}}
and replace it with:
Subject: {{var data.subject}}
- Now go to System => Configuration and select Contacts. Then under the Email Options, select your new template under the Email Template drop-down and click on Save Config button.
I did this in Magento 2.2.X
1) Put this in your app/design/frontend/[theme-provider]/[theme-name]/Magento_Contact/templates/form.phtml and modify it to your liking, for example add a new select box as follows:
<div class="field region input-box">
<label for="dropdown"><span><?php /* @escapeNotVerified */ echo __('Region') ?></span></label>
<select name="dropdown" id="dropdown">
<option value="selected">select…</option>
<option name="retail" value="Retail">Retail</option>
<option name="wholesale" value="Wholesale">Wholesale</option>
<option name="customerservice" value="CustomerService">Customer Service</option>
<option name="logistics" value="logistics">Logistics</option>
<option name="general" value="General">General</option>
</select>
</div>
2) Run this code:
php bin/magento setup:upgrade
php bin/magento cache:flush
3) In your dashboard go to > Marketing > Email Templates, you need to create a new template if you don't have one
4) In Load default template choose a template in this case Contact Form and click on the button Load Template.
5) Add a name to the template.
6) In template content Open a space in the code and place this:
<tr>
<td><b>{{trans "Region"}}</b></td>
<td>{{var data.dropdown}}</td>
</tr>
7) in your dashboard go to > STORES > Configuration > General Contacts > Email Options > Email Template, choose your new email template and save.
Best Answer
First, open the
form.phtml
file located in your theme./magento_root/app/design/frontend/VENDER_NAME/YOUR_THEME/Magento_Contact/templates/form.phtml, then add subject field to this contact form:
Add the above field in the form.
As far as Magento is concerned, it doesn't care what fields we add to this form. It is written in such a way that it accepts all of the field posted for processing and send that out to the transactional e-mail form that you create. Now, 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 will see:
Add your new field before Name: {{trans "Name: %name" name=$data.name}} so that now it should looks like this:
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".