DEV Community

suthanalley
suthanalley

Posted on

How to Add Custom Fields to Magento 2 Contact Page

Firstly create a module

create module.xml in app/code/Magenticians/Modulecontact/etc and put the code:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magenticians_Modulecontact" setup_version="1.0.1">
</module>
</config>
Enter fullscreen mode Exit fullscreen mode

For module registration, create registration.php in app/code/Magenticians/Modulecontact and put the code:

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Magenticians_Modulecontact',
__DIR__
);
Enter fullscreen mode Exit fullscreen mode

Now override and add new field in Magento 2 contact form.

Copy form.phtml file from the vendor/magento/module-contact/view/frontend/templates and paste in app/code/Magenticians/Modulecontact/view/frontend/templates.
To add a new field to the contact form, open form.phtml file and add the following code:

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

Complete Guide: https://magenticians.com/add-custom-field-in-magento-2-contact-page/

Top comments (0)