Do You Want to Learn Marketing or Coding? Do You Want to Learn Marketing or Coding?

Magento Make Multiple Contact Us Forms and redirect them to seperate thank you pages.

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn

I currently have a customer that needs multiple contact us forms that redirect to different thank you pages. After a long time I finally figured out how to make separate contact us pages and redirect the submission to a specific thank you page. Let’s say you want to make a form for your wholesalers that will be on wholesales.html and redirect them to a very special thankyou.html page.

First navigate to your skin template’s contact form files for me it is app/design/frontend/base/default/template/contacts

Once in that directory copy forms.phtml and name it something like form-wholesalers.phtml

Next open up this new .phtml file and scroll down to the bottom till you see:

Next ABOVE that line of code add the following:

Basically we are simply creating a input called custom_redirect. We are hiding it from the public so that on one can see it. You will see in the code a part that says your/redirect/url.html. Here you can add the url you want the form to redirect to once it is posted. For example if I wanted the site to redirect to milessebesta.com/thank/you.html it would be:
value="your/redirect/url.html" />
Next save the file and go to app/code/core/Mage/Contacts/controllers and open the IndexController.php file.
IndexController.php controls the actions of your contact us form. So in it we are going to make some changes. Make sure you copy this file incase anything strange happens. Always make back-ups.

Scroll down and find the following code:

if (!$mailTemplate->getSentSuccess()) {
throw new Exception();
}

$translate->setTranslateInline(true);

Mage::getSingleton('customer/session')->addSuccess(Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.'));
$this->_redirect('*/*/');

return;

This section controls what your system does once a contact us form is successfully ran. Now if you wanted to redirect all of your contact us forms to let’s say thankyou.hmtl you could just change
$this->_redirect(‘*/*/’); to $this->_redirect(‘thankyou.html’); but that is too limited for us. We want to be able to make 100’s of forms and send them to all different thank you pages. To do this we need to change this section.

Here is the code we want:

if (!$mailTemplate->getSentSuccess()) {
throw new Exception();
}

$translate->setTranslateInline(true);

if (isset($post['custom_redirect'])) { // If custom_redirect is set
$redirect_url = $post['custom_redirect']; // Get the URL you want to redirect to
$redirect_too = (explode(".com/", $redirect_url)); // Explode the Url
Mage::getSingleton('customer/session')->addSuccess(Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.'));
$this->_redirect($redirect_too[1]); // Redirect to everything everything after .com
return;

} else {

Mage::getSingleton('customer/session')->addSuccess(Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.'));
$this->_redirect('*/*/');

return;

First we see if we have set a custom_redirect form, it it exists then it moves on, if not it takes us to the default contact us page. We do this through this line of code:
if (isset($post['custom_redirect'])) { // If custom_redirect is set
Next we get the custom redirect url and set it to $redirect_url through the following:
$redirect_url = $post['custom_redirect]; // Get the URL you want to redirect to
Next we break the domain apart by finding the .com/. We have to do this because our $this->redirect needs to be to a directory not the full url. If we add the full url we will get an error.
So we explode the string $redirect_url by searching for .com/. So for milessebesta.com/thankyou.html we only want thankyou.html. That is why we must explode the the domain string. We do this by using this:
$redirect_too = (explode(".com/", $redirect_url)); // Explode the Url
Next we add back in the default session information in case you want to use that silly green box on your thank you page for some reason. That section is this code, most of us won’t need it:
Mage::getSingleton('customer/session')->addSuccess(Mage::helper('contacts')->__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.'));

Next we enter our redirect. We tell the system to use everything after “.com/” in a domain name through the use of $redirect_too[1] we place this within $this->_redirect(); because that is the code magento requires. Here is the complete line:
$this->_redirect($redirect_too[1]); // Redirect to everything everything after .com
Finally we execute the return; and close the if statement through }.

Now we are basically done with the coding. But, we need to add the form to a page to test it! So login to your magento backend and go to the cms section. Add a new page and name it whatever you want so for me it would be wholesalers.html

In the body of the page enter the following:

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

Replace the your_form_here.phtml to whatever custom form you created so for me it would be form-wholesalers.phtml.

Save the page. Next create the thank you page you wanted to the direct to, like thankyou.html, if it doesn’t exist already, and put the information you want on it. Next navigate to your custom form page and test it! If done correctly it should take you to your custom thankyou page.

If you have any questions please ask! If you would like to know how to make custom fields for your custom form check out this link http://www.magentocommerce.com/wiki/4_-_themes_and_template_customization/contacts/add_fields_to_contact_form

Download example files of the working code.

These are modified magento enterprise files. I will work on community edition files soon. These examples are slightly modified and don’t call for in the following:

value="your/redirect/url.html" />

Instead you just set the value=”redirect/url.html”
It also removed the explosion section of the code. It assumes that you are staying on the same domain not switching over to a different base URL

Also be sure to check out my web site portfolio and tell me what you think!

I Really Need

Reviewing nerdy stuff is expensive.

Consider clicking on this ad & buying that item you REALLY wanted from Amazon.

It helps a ton!

Leave Your Thoughts