Magento, passing product name to contact form

Magento, passing product name to contact form

September 3, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento, passing product name to contact form

Magento: Passing Product Name to Contact Form

Hello! I’m glad you’re exploring how to integrate Magento’s contact form into product view pages while also passing along relevant product information. This is a common task for many storefront customizations.

Understanding the Magento Contact Form Structure

The built-in Magento contact form is designed to gather general customer inquiries. It includes fields for name, email, telephone, and a comment section.

By default, this form is placed on its own page under Contacts > Contact Us in the frontend menu. When you view the source code of this page, you’ll see that the form’s structure looks something like this:

<form id="contact-form" action="<?php echo $this->getFormAction(); ?>" method="post">
  <!-- Form fields go here -->
</form>

The action attribute points to the Mage_Contacts_IndexController::postAction() function, which handles form submissions.

Embedding the Contact Form in Product Pages

You’ve already made some progress by copying the contact form template into your theme’s directory and embedding it using a custom layout update. This allows you to place the contact form on specific product pages.

However, since this is a one-off solution for each product, it might not be the most efficient approach if you need to apply this to many products. For a more scalable solution, consider using an event-observer pattern or extending the core Mage_Catalog_Block_Product_View block.

Passing Product Information to the Contact Form

Your goal is to send the current product’s name as an additional parameter when the contact form is submitted. There are a few ways to achieve this:

  1. Hidden Input Field: You can add a hidden input field for the product name in your custom form.phtml template.

    <input type="hidden" id="product_name" value="<?php echo $_product->getName(); ?>">
    
  2. Session Variable: You could also save the product name in session before displaying the form and then retrieve it in the controller when processing the form submission. However, this approach adds unnecessary complexity.

  3. Layout Update XML: In your custom layout update XML, you can set a block’s template attribute to a new template file that includes the hidden input field for the product name:

    <reference name="content">
      <block type="core/template" name="customer_request" template="contacts/form.phtml"/>
    </reference>
    

Passing Parameters to the Contact Form Action

Once you have the product name in your form as a hidden input field, it will be sent along with other form data when the form is submitted.

The Mage_Contacts_IndexController::postAction() function receives this information and processes it. To access the product name in this function, simply retrieve it from the $this->getRequest()->getParams() array:

$productName = $this->getRequest()->getParam('product_name');

Best Practices for Customizing Magento Forms

Remember a few key best practices when customizing forms in Magento:

  1. Always make a copy of the core template before modifying it to avoid conflicts during updates.
  2. Use layout updates or event-observer patterns when possible instead of direct block modifications for better maintainability and scalability.
  3. Test your changes thoroughly on different browser types and devices.

With these steps, you should be able to successfully integrate the Magento contact form into product pages while sending the product name as additional data. This customization can greatly enhance the user experience by allowing customers to provide more specific feedback about products they’re interested in.

If you found this tutorial helpful, please share it with your fellow developers and check out our other Magento tutorials on Laravel Company. Happy coding!

Catalog Extensions Themes Email

Our network