Categories: How ToMagento 2

How to override contact form in Magento 2 theme

Magento’s design theme system has grown significantly over the years. Compared to Magento 1.x, there are so many distinctive changes and improvements were done in default theme structure also now it allows creating unlimited parent themes as well as now its support latest languages like HTML5, CSS3, and jQuery. When it comes to giving a personalized touch to Magento store, instead of modifying default theme files overriding file is the safest way to change the layout & We also never recommended to modify core files. Also, check out the Quick Contact Form extension for Magento 2 which helps to embed the contact button on all the pages of your website.
Recently working on Magento custom development requirement, we came across the requirement of adding an element on the frontend form. At that time, we have overridden child theme contact form which we have shared below.
Firstly, you need to create ‘contact_index_index.xml‘ the file at following location.
app\design\frontend\Your Theme\Magento_Contact\layout\contact_index_index.xml



   
     
        
             Magento_Contact::form.phtml
         
     
   

Once you are done, you have to create another file ‘form.phtml‘ at below location and inside that you have to put your contact form code which you want to override.
app\design\frontend\Your_Theme\Magento_Contact\templates\form.phtml
Taada, you have successfully override contact form in your Magento 2 child theme. You can even customize this code according to your need of adding and removing contact form elements.
Comment down below if you are looking for help regarding this code & don’t forget to rate this blog.
Happy Overriding!

Click to rate this post!
[Total: 10 Average: 4.6]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

View Comments

  • Hi,

    Thanks for the helpful post. Question: how would I reference the custom contact form block as a namespace? i.e., what's the equivalent of Magento\Contact\Block\ContactForm here?

    • Then you can use another method of specify template with the class like this one
      < block class="Vendor\Extension\Block\Yourblock" name="yourblockname" template="Vendor_Extension::form.phtml">

Recent Posts

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

3 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

4 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

5 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

7 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago