1

I'm using the same form for a few pages.

{{block type="core/template" name="contactForm" form_action="/contacts/index/post" template="contacts/form.phtml"}}

Is it possible to somehow detect from which page the email was sent?

2

You can edit the form.phtml file, add an extra hidden field and output the url if you like.

Get the URL with this:

<?php
$currentUrl = Mage::helper('core/url')->getCurrentUrl();
$url = Mage::getSingleton('core/url')->parseUrl($currentUrl);
$path = $url->getPath();
?>

Place the above php code above the first line of html code in your

app/design/frontend/{package}/{theme}/template/contacts/form.phtml

Somewhere within the <form> </form> tag insert a hidden field and output the $url variable (here it's based on the rwd theme):

<input type="hidden" name="url" id="url" value="<?php echo $path; ?>" />

So, the complete form.phtml looks like this:

    <?php
    /**
     * Magento
     *
     * NOTICE OF LICENSE
     *
     * This source file is subject to the Academic Free License (AFL 3.0)
     * that is bundled with this package in the file LICENSE_AFL.txt.
     * It is also available through the world-wide-web at this URL:
     * http://opensource.org/licenses/afl-3.0.php
     * If you did not receive a copy of the license and are unable to
     * obtain it through the world-wide-web, please send an email
     * to license@magento.com so we can send you a copy immediately.
     *
     * DISCLAIMER
     *
     * Do not edit or add to this file if you wish to upgrade Magento to newer
     * versions in the future. If you wish to customize Magento for your
     * needs please refer to http://www.magento.com for more information.
     *
     * @category    design
     * @package     rwd_default
     * @copyright   Copyright (c) 2006-2014 X.commerce, Inc. (http://www.magento.com)
     * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
     */
    ?>
    <?php
        $currentUrl = Mage::helper('core/url')->getCurrentUrl();
        $url = Mage::getSingleton('core/url')->parseUrl($currentUrl);
        $path = $url->getPath();
    ?>
    <div id="messages_product_view"><?php echo $this->getMessagesBlock()->toHtml() ?></div>
    <div class="page-title">
        <h1><?php echo Mage::helper('contacts')->__('Contact Us') ?></h1>
    </div>
    <form action="<?php echo $this->getFormAction(); ?>" id="contactForm" method="post" class="scaffold-form">
        <div class="fieldset">
            <h2 class="legend"><?php echo Mage::helper('contacts')->__('Contact Information') ?></h2>
            <p class="required"><?php echo Mage::helper('contacts')->__('* Required Fields') ?></p>
            <ul class="form-list">
                <li class="fields">
                    <div class="field">
                        <label for="name" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Name') ?></label>
                        <div class="input-box">
                            <input name="name" id="name" title="<?php echo Mage::helper('contacts')->__('Name') ?>" value="<?php echo $this->escapeHtml($this->helper('contacts')->getUserName()) ?>" class="input-text required-entry" type="text" />
                        </div>
                    </div>
                    <div class="field">
                        <label for="email" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Email') ?></label>
                        <div class="input-box">
                            <input name="email" id="email" title="<?php echo Mage::helper('contacts')->__('Email') ?>" value="<?php echo $this->escapeHtml($this->helper('contacts')->getUserEmail()) ?>" class="input-text required-entry validate-email" type="email" autocapitalize="off" autocorrect="off" spellcheck="false" />
                        </div>
                    </div>
                </li>
                <li>
                    <label for="telephone"><?php echo Mage::helper('contacts')->__('Telephone') ?></label>
                    <div class="input-box">
                        <input name="telephone" id="telephone" title="<?php echo Mage::helper('contacts')->__('Telephone') ?>" value="" class="input-text" type="tel" />
                    </div>
                </li>
                <li class="wide">
                    <label for="comment" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Comment') ?></label>
                    <div class="input-box">
                        <textarea name="comment" id="comment" title="<?php echo Mage::helper('contacts')->__('Comment') ?>" class="required-entry input-text" cols="5" rows="3"></textarea>
                    </div>
                </li>
            </ul>
        </div>
        <div class="buttons-set">
            <input type="text" name="hideit" id="hideit" value="" style="display:none !important;" />
            <input type="hidden" name="url" id="url" value="<?php echo $path; ?>" />
            <button type="submit" title="<?php echo Mage::helper('contacts')->__('Submit') ?>" class="button"><span><span><?php echo Mage::helper('contacts')->__('Submit') ?></span></span></button>
        </div>
    </form>
    <script type="text/javascript">
    //<![CDATA[
        var contactForm = new VarienForm('contactForm', true);
    //]]>
    </script>

Here is how to add the extra field (and show it in the transaction email): http://www.magentocommerce.com/wiki/4_-_themes_and_template_customization/contacts/add_fields_to_contact_form

Now add your Variable to your email template, following the above guide:

Sent from: {{var data.url}}
  • I'm sorry, I'm a newbie. How do I create a hidden field? Which specific code? And in which file do I add this: $currentUrl = Mage::helper('core/url')->getCurrentUrl(); $url = Mage::getSingleton('core/url')->parseUrl($currentUrl); $path = $url->getPath(); – Joana Rijo Nov 27 '14 at 10:31
  • I've edited my answer for you ;) – tecjam Nov 27 '14 at 10:42
  • style="display:none !important;" does not make the field hidden, it hides it, at most. Why don't you use <input type="hidden" /> instead ? – Julien Lachal Nov 27 '14 at 13:03
  • You're right. I was in a hurry and it was quick and dirty hack, simply duplicating the existing <input type="text" name="hideit" id="hideit" value="" style="display:none !important;" /> which is in the template already. To be fair, anyone messing with the template .phtml files should have some basic grasp of html. – tecjam Nov 27 '14 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.