Home > Magento Tutorials > Product Name Prefills Contact Form

Product Name Prefills Contact Form

Posted on: 26th Apr 2010 By: Adam Moss 8 Comments

Following on from earlier Magento blog posts where I’ve described how some clients may not want to actually sell their products online, but rather use the architecture of Magento as a CMS – there’s a very simple way of converting these product pages into online enquiries. Trust me, I’m not flogging a dead horse with all this talk of using Magento, but not as a shop. It really can work! A neat touch is to pre-fill the existing Magento contact form with details about the product that you’re on – this could include the product name, price and any attributes you could care to mention. Here’s how to do it:

Open your product view page: app/design/frontend/base/modern/template/product/view.phtml

<form method="post" action="/contacts">
<input name="productname" type="hidden" value="<?php echo $_helper->productAttribute($_product, $this->htmlEscape($_product->getName()), 'name') ?>" />
<input type="image" src="/images/enquire.gif" />
</form>

If you add something similar to the form above, you’ll have hidden fields pre-filled with the Product Name data, and a new ‘Enquire Online’ button. Also notice that the form’s action value points to the contact us page that Magento creates for you.

Now if you open the contacts page: app/design/frontend/base/default/template/contacts/form.phtml

<label for="subject"><?php echo $this->__('Subject') ?></label><br />
<input name="subject" id="subject" title="<?php echo $this->__('Subject') ?>" value="<?php if (isset($_POST["productname"])) { echo $_POST["productname"]; } ?>" type="text"/>
</div>

In this instance I have entered the Product Name into the ‘Subject’ field of the contact form, but some very important PHP is involved. The “if(isset” conditional is essential in not breaking your contact form in circumstances where people visit your contact page without going through the product first. If you apply the exact same rules to any data on the product page you can create a very seamless and user-friendly online enquiries process.

Who know, you then might get to use Magento as an actual ecommerce website design shop!!

8 Responses to “ Product Name Prefills Contact Form ”

  1. Jess
    #1 | 1st May 2010

    Can you please explain this instruction step by step, because i tried it my self and it wont work.

  2. Michael Angrave
    #2 | 18th May 2010

    Thanks for this, works excellently, the only problem I had was, at first I accidentally copied the form code into another form, which seemed to just confuse the action, making it think you were still adding the product to the cart.

    This is helpful, even if you do still want your customers to be able to purchase as well.

  3. Adam
    #3 | 19th May 2010

    Jess, I should probably mention that you want this code to be outside of the main shopping cart form, otherwise it won’t work. The code I have provided has worked without problem for me – send me the source code of the product and contact pages and I’ll take a look.

  4. nuyw
    #4 | 11th October 2010

    Thank you so much for such a DIRECT answer which I can’t found on Magento forum.
    This is EXACTLY what I’m looking for.

    But for those person which already using “Contacts” for something else do you have any suggestion?

    Cheers,
    Nuy.

  5. Doofah
    #5 | 16th December 2010

    For those of you who want to have a button that works where the Add To Cart button should be, try this:

    
    	            <button onclick="setLocation('/contacts?productname=productAttribute($_product, $this->htmlEscape($_product->getName()), 'name') ; ?>'); return(false);" title="/contacts?product=productAttribute($_product, $this->htmlEscape($_product->getName()), 'name') ; ?>" class="button btn-cart">Enquire or Make A Booking
    
  6. leacher
    #6 | 30th July 2011

    been looking for it but it doesn’t seem to work. using ver. 1.5.1.0
    and theme Magento absolute, followed your directions but no result

    any help?

  7. Daniel
    #7 | 19th September 2011

    I would love to use this including a contact form in easytabs – is there a way to have it working in Magento 1.5.1?

  8. Adam Moss
    #8 | 19th September 2011

    Daniel: what do you mean by working in easytabs?

    Leacher: the version of Magento should have no effect on this, it’s simply just posting product data to another page. Are you still having issues with this?

Post A Comment

Your comments:
Enclose code snippets within the appropriate tags: [php][/php]   [js][/js]   [xml][/xml]   [css][/css]   [html][/html]
E.g: [php]<?php echo "hello world"; ?>[/php]

Search Blog

Archives

For the record...

Views & opinions in this blog are those of the individual and do not necessarily reflect those of E-commerce Web Design or the Creare Group.