Magento 2: How to add custom field in contact page
-
13-04-2021 - |
Frage
I have added in Magento_Contact
module custom field with order number:
<div class="field telephone">
<div class="control">
<input placeholder="<?php echo __('Your Phone') ?>" name="telephone" id="telephone"
title="<?php /* @escapeNotVerified */
echo __('Phone Number') ?>" value="" class="input-text" type="text"/>
</div>
</div>
<div class="field ordernumber">
<div class="control">
<input placeholder="<?php echo __('Order Number') ?>" name="ordernumber" id="ordernumber"
title="<?php /* @escapeNotVerified */
echo __('Order Number') ?>" value="" class="input-text" type="text"/>
</div>
</div>
screen: https://prnt.sc/s5fgtu
But when I sent a message via this form after filling this custom field (order number), when I receive a message from this contact module then this field is not displayed.
screen: https://prnt.sc/s5fh54
Any solution how to do?
Lösung
This code is correct :-
<div class="field ordernumber">
<div class="control">
<input placeholder="<?php echo __('Order Number') ?>" name="ordernumber" id="ordernumber"
title="<?php /* @escapeNotVerified */
echo __('Order Number') ?>" value="" class="input-text" type="text"/>
</div>
</div>
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:
{{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}}
Add your new field and this code :-
{{template config_path="design/email/header_template"}}
<table class="message-details">
<tr>
<td><strong>{{trans "Name"}}</strong></td>
<td>{{var data.name}}</td>
</tr>
<tr>
<td><strong>{{trans "Email"}}</strong></td>
<td>{{var data.email}}</td>
</tr>
<tr>
<td><strong>{{trans "Phone"}}</strong></td>
<td>{{var data.telephone}}</td>
</tr>
<tr>
<td><strong>{{trans "ordernumber"}}</strong></td>
<td>{{var data.ordernumber}}</td>
</tr>
</table>
<p><strong>{{trans "Message"}}</strong></p>
<p>{{var data.comment}}</p>
{{template config_path="design/email/footer_template"}}
And save template. Now you 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 Button.
Hope help you
Thanks ...