magento custom form on product page

magento custom form on product page

July 20, 2026 · Illustrated author avatarBy Stefan Bogdanescu
magento custom form on product page

Magento Custom Form on Product Page: A Comprehensive Guide

If you’re new to Magento development and looking to add a custom form to your product pages, this guide will provide you with the necessary conceptual understanding and practical steps.

Understanding Your Requirement

You want to create a custom form that appears at the bottom of some (but not all) product pages. This form should collect user input (email, size, color), allowing customers to request specific variations of in-stock products. The form data should then be sent via email to the store admin.

Should You Customize or Create Fresh?

This is a common question for Magento beginners. In this case, customizing the existing Contact us form isn’t the best approach due to its lack of customization and potential confusion for users.

Creating a fresh custom form in a new module provides better control over the functionality, layout, and appearance. It also improves code organization and maintainability.

Creating Your Custom Form Module

  1. Create a New Module

    First, create a new custom module following Magento’s naming convention (namespace_module). For this example, let’s use LaravelCompany_ProductRequest.

  2. Define the Form Fields

    In your module’s etc/frontend/di.xml file, define your form fields:

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <type name="Magento\Framework\View\Element\Template">
            <arguments>
                <argument name="template" xsi:type="string">LaravelCompany_ProductRequest::product_request.phtml</argument>
            </arguments>
        </type>
    </config>
    
  3. Create the Form Template

    In your module’s view/frontend/templates folder, create a file named product_request.phtml. This will contain your custom form HTML.

    <form action="<?= $block->getFormUrl() ?>" method="post">
        <?= $block->getBlockHtml('formkey') ?>
        <input type="hidden" name="product_id" value="<?= $block->getProductId() ?>">
        <label for="email">Email:</label>
        <input type="email" id="email" name="email" required>
        <label for="size">Size:</label>
        <select id="size" name="size">
            <!-- Options here -->
        </select>
        <label for="color">Color:</label>
        <input type="text" id="color" name="color">
        <button type="submit">Request Product</button>
    </form>
    
  4. Handle Form Submission

    Create a controller to handle the form submission:

    public function execute()
    {
        $post = $this->getRequest()->getPostValue();
    
        $email = $post['email'];
        $size = $post['size'];
        $color = $post['color'];
    
        // Your email sending logic here
    
        return $this->_redirect('catalog/product/view/id/' . $post['product_id']);
    }
    

Conclusion: A Solid Foundation for Custom Magento Forms

Creating a custom form on the product page is a great example of extending Magento’s core functionality. By following this guide, you’ve learned how to create a new module, define a form template, and handle form submissions.

Remember, Magento development requires a solid understanding of PHP, XML, and object-oriented programming. Always refer to the official Magento Developer Documentation for detailed instructions.

At LaravelCompany (https://laravelcompany.com), we specialize in custom Magento development services. If you need professional assistance with form development or any other Magento project, feel free to reach out!

The key takeaway is: always create fresh modules for custom functionality. It keeps your code organized and makes maintenance much easier in the long run.

Happy coding!

Catalog Extensions Themes Admin

Our network