2 minutes November 22, 2024

Vuestorefront + Magento 2: How to create a contact form and display it on the contact page

Before we begin, it’s assumed that you have basic Vue and Magento 2 knowledge.

By default, Vuestorefront doesn’t include a form on the contact page. Here, we’ll discuss how to create one in simple steps.

Create a contact form component in Vuestorefront and display it on the Contact Us page.

The contact page is a CMS page, as the main contents are loaded from the Magento 2 CMS. In your Vuestorefront project directory, locate the CMS.vue component in the pages directory.

In the code above, we have added the necessary codes to display the contents from the backend. Additionally, we have added a new component to display the form. In JavaScript, we have added the validations and handlers for the form submission actions. After submission, the form will reset and show a success notification.

Please refer to the form component located in components/ContactForm/ContactForm.vue for more details.

Here we are adding components/ContactForm/types.ts for the ContactFormFields to be sent over Magento 2 API via GraphQL mutation.

The Vue Storefront part has been completed. Once you run the yarn build in your Vue Storefront, the form will be displayed on the “Contact Us” page. Next, we need to create a new module to handle the contact form mutation. Create a custom module and add the following schema.graphqls file in the etc folder of the Magento 2 module.

Conclusion

You are done. Now the process is complete, you have to run the below commands in the Magento 2 root directory to activate the module:

If you are looking for a technical expert to handle this feel free to Contact us.

blog
Greetings! I'm Aneesh Sreedharan, CEO of 2Hats Logic Solutions. At 2Hats Logic Solutions, we are dedicated to providing technical expertise and resolving your concerns in the world of technology. Our blog page serves as a resource where we share insights and experiences, offering valuable perspectives on your queries.
Aneesh ceo
Aneesh Sreedharan
Founder & CEO, 2Hats Logic Solutions
Subscribe to our Newsletter
Aneesh ceo

    Stay In The Loop!

    Subscribe to our newsletter and learn about the latest digital trends.