2

I am working for a Magento based website in which my for contact us page when I fill the form and click on the submit, i got the message of "thankyou" on same page but the url is changing from example.com/contact/ to example.com/contact/index/ (after click on the submit button).

I want to create a custom page of thank you in magento.

e.g. https://www.example.com/thank-you

How can I do this?

2
  • Need override contact controller in local
    – Abdul
    Commented Dec 18, 2015 at 13:14
  • How to do that?
    – Rajat
    Commented Dec 18, 2015 at 13:16

2 Answers 2

1

This is a two part answer. Part 1 as Abdul said you have to handle the controller so you can redirect them to the custom page after its successful. Part 1, lets take over the contacts controller, then we can handle the redirect to a custom page. Create a custom module called Contacts Step 1 create the module declaration in app/etc/modules/ and call it something like Rajat_Contacts.xml

    <?xml version="1.0"?>
        <config>
            <modules>
                <Rajat_Contacts>
                <active>true</active>
                <codePool>local</codePool>
                </Rajat_Contacts>
            </modules>
        </config>

Now create the module in app/code/local Create a folder called Rajat and inside that two folders etc/ and controllers

Inside app/code/local/Rajat/Contacts/etc/ create a file called config.xml and put this

    <?xml version="1.0"?>
    <config>
        <modules>
            <Rajat_Contacts>
                <version>1.0.0</version>
            </Rajat_Contacts>
        </modules>
        <frontend>
            <routers>
                <contacts>
                    <args>
                        <modules>
                            <rajat_contacts before="Mage_Contacts">Rajat_Contacts</rajat_contacts>
                        </modules>
                    </args>
                </contacts>
            </routers>
        </frontend>
    </config>

Now in app/code/local/Rajat/Contacts/controllers create a file called app/code/local/Rajat/Contacts/controllers/IndexController.php and here is the contents:

<?php

    require_once(Mage::getModuleDir('controllers', 'Mage_Contacts') . DS . 'IndexController.php');

    class Rajat_Contacts_IndexController extends Mage_Contacts_IndexController
    {
    public function postAction()
    {
        $post = $this->getRequest()->getPost();
        if ( $post ) {
            $translate = Mage::getSingleton('core/translate');
            /* @var $translate Mage_Core_Model_Translate */
            $translate->setTranslateInline(false);
            try {
                $postObject = new Varien_Object();
                $postObject->setData($post);

                $error = false;

                if (!Zend_Validate::is(trim($post['name']) , 'NotEmpty')) {
                    $error = true;
                }

                if (!Zend_Validate::is(trim($post['comment']) , 'NotEmpty')) {
                    $error = true;
                }

                if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
                    $error = true;
                }

                if (Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) {
                    $error = true;
                }

                if ($error) {
                    throw new Exception();
                }
                $mailTemplate = Mage::getModel('core/email_template');
                /* @var $mailTemplate Mage_Core_Model_Email_Template */
                $mailTemplate->setDesignConfig(array('area' => 'frontend'))
                    ->setReplyTo($post['email'])
                    ->sendTransactional(
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
                        null,
                        array('data' => $postObject)
                    );

                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.'));
                $thank_you_url = Mage::getUrl('thank-you');
                $this->_redirectUrl($thank_you_url);

                return;
            } catch (Exception $e) {
                $translate->setTranslateInline(true);

                Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Unable to submit your request. Please, try again later'));
                $this->_redirect('*/*/');
                return;
            }

        } else {
            $this->_redirect('*/*/');
        }
    }


}

Now, use the built int CMS and create a page with the url-key of thank-you so it matches your redirectUrl we just put in the code.

That should take the customers through the same controller, but you are handling the url redirect after its successful.

1

you need to rewrite app/code/core/Mage/Contacts/controller/IndexController.

app/etc/modules/Namespace_ModuleName.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Namespace_ModuleName>
      <active>true</active>
      <codePool>local</codePool>
      <version>0.1.0</version>
    </Namespace_ModuleName>
  </modules>
</config>

app/code/local/Namespace/Modulename/etc/config.xml file of module

<?xml version="1.0"?>
<config>
  <modules>
    <Namespace_ModuleName>
      <version>0.1.0</version>
    </Namespace_ModuleName>
  </modules>
  <frontend>
    <routers>
      <modulename>
        <use>standard</use>
          <args>
            <module>Namespace_ModuleName</module>
            <frontName>modulename</frontName>
          </args>
      </modulename>
    </routers>
  </frontend>
  <global>
        <rewrite>        
            <namespace_modulename_contacts_indexcontroller>
                <from><![CDATA[#^/contacts/index/#]]></from> <!-- Mage_Contacts_IndexController  -->
                <to>/modulename/contacts_index/</to> <!-- Namespace_ModuleName_Contacts_IndexController  -->
            </namespace_modulename_contacts_indexcontroller>
        </rewrite>
    <helpers>
      <modulename>
        <class>Namespace_ModuleName_Helper</class>
      </modulename>
    </helpers>
  </global>
  <admin>
    <routers>
      <modulename>
        <use>admin</use>
        <args>
          <module>Namespace_ModuleName</module>
          <frontName>admin_modulename</frontName>
        </args>
      </modulename>
    </routers>
  </admin>
</config> 

Create app/code/local/Namespace/Modulename/Helper/Data.php

<?php
class Namespace_ModuleName_Helper_Data extends Mage_Core_Helper_Abstract
{
}

Create controller app/code/local/Namespace/Modulename/controllers/Contacts/IndexController.php

<?php
require_once "Mage/Contacts/controllers/IndexController.php";  
class Namespace_ModuleName_Contacts_IndexController extends Mage_Contacts_IndexController{
     public function postAction()
    {
        $post = $this->getRequest()->getPost();
        if ( $post ) {
            $translate = Mage::getSingleton('core/translate');
            /* @var $translate Mage_Core_Model_Translate */
            $translate->setTranslateInline(false);
            try {
                $postObject = new Varien_Object();
                $postObject->setData($post);

                $error = false;

                if (!Zend_Validate::is(trim($post['name']) , 'NotEmpty')) {
                    $error = true;
                }

                if (!Zend_Validate::is(trim($post['comment']) , 'NotEmpty')) {
                    $error = true;
                }

                if (!Zend_Validate::is(trim($post['email']), 'EmailAddress')) {
                    $error = true;
                }

                if (Zend_Validate::is(trim($post['hideit']), 'NotEmpty')) {
                    $error = true;
                }

                if ($error) {
                    throw new Exception();
                }
                $mailTemplate = Mage::getModel('core/email_template');
                /* @var $mailTemplate Mage_Core_Model_Email_Template */
                $mailTemplate->setDesignConfig(array('area' => 'frontend'))
                    ->setReplyTo($post['email'])
                    ->sendTransactional(
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_SENDER),
                        Mage::getStoreConfig(self::XML_PATH_EMAIL_RECIPIENT),
                        null,
                        array('data' => $postObject)
                    );

                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.'));
                $url=Mage::getUrl('thank-you');
                $this->_redirect($url);

                return;
            } catch (Exception $e) {
                $translate->setTranslateInline(true);

                Mage::getSingleton('customer/session')->addError(Mage::helper('contacts')->__('Unable to submit your request. Please, try again later'));
                $this->_redirect('*/*/');
                return;
            }

        } else {
            $url=Mage::getUrl('thank-you'); //redirect to thank you page
                $this->_redirect($url);
        }
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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