4

I'm trying to get the Magento 2 Contact form to redirect not to /contact/index but someplace else. Here is what I have so far:

We use a custom theme, in which I have created a the following folders

Magento_Contact -email -submitted_form.html -templates - form.phtml

I've edited the form.phtml to add / edit the fields as required.

I've created a content block and added the form to it with:

{{block class="Magento\Contact\Block\ContactForm" name="contactForm" template="Magento_Contact::form.phtml"}}

And I've created a CMS Page with this block in it, using

{{block class="Magento\\Cms\\Block\\Block" block_id="myContact"}}

Now I want to the user to stay on that CMS Page after submitting the form, so I presume I will need to change $this->_redirect('contact/index'); to $this->_redirect('*/*/'); in the modules post controller.

I was wondering if there wasn't a quicker way to add the redirect, maybe as a block comment or similar.

Thank you.

| improve this question | | | | |
  • did you find a solution to this? – Jimmery Apr 20 '16 at 14:28
  • No, I simply created a cms page under 'contact/index' with a 'Thank you and success message' and then redirected the user back to the form page using a very hacky cms block containing a meta refresh ... Now I would use a plugin with an after action, but this is no longer relevant. – tecjam Apr 20 '16 at 15:31
4

You can implement Magento 2 after method plugin for execute method of Post controller of Contact module. And create CMS page with 'contact/thanks' URL and redirect to the page in your plugin. But in that case you need custom module to place your plugin.

<?php

namespace YourModule\ContactUsRedirect\Plugin;

class PostPlugin
{
    /**
     * @var \Magento\Framework\App\Request\DataPersistorInterface
     */
    private $_dataPersistor;

    /**
     * @var \Magento\Framework\App\Response\RedirectInterface
     */
    private $_redirect;

    /**
     * PostPlugin constructor.
     * @param \Magento\Framework\App\Request\DataPersistorInterface $dataPersistor
     * @param \Magento\Framework\App\Response\RedirectInterface $redirect
     */
    public function __construct(
        \Magento\Framework\App\Request\DataPersistorInterface $dataPersistor,
        \Magento\Framework\App\Response\RedirectInterface $redirect
    ) {
        $this->_dataPersistor = $dataPersistor;
        $this->_redirect = $redirect;
    }

    public function afterExecute(\Magento\Contact\Controller\Index\Post $subject, $result)
    {
        $post = $subject->getRequest()->getPostValue();

        if (!$post) {
            return;
        }

        if (!$this->_dataPersistor->get('contact_us')) {
            $this->_redirect->redirect($subject->getResponse(), 'contact/thanks');
        }
    }
}
| improve this answer | | | | |
2

Old question but I found this through search. @Design was 90% correct but apparently didn't test their answer.

First copy the Magento_Contact form.phtml to your theme.

app/design/frontend/{Namespace}/{Theme}/Magento_Contact/templates/form.phtml

Next, append the following script to the new file

<script>
require(
    ['jquery'],
    function($) {
        $('#contact-form').on('submit',function(e){
            e.preventDefault();
            var form = this;
            $.ajax({
                url: "<?php /* @escapeNotVerified */ echo $block->getFormAction(); ?>",
                method : "POST",
                data: $(form).serialize(),
                success: function(){ 
                    window.location = '/redirect';
                },
                error: function(){ 
                    console.error("Some error occurred, make sure Contact Form is enabled in Magento.");
                }
            });

        });
    }
);
</script>

Change '/redirect' to your desired redirect target. You can also just replace the form with a success message. Here's a simple example of what I needed:

success: function(){ 
    form.outerHTML = "Thank you for contacting us, we will be in touch shortly!";
}
| improve this answer | | | | |
  • 1
    Thanks! I tried this one and it's working as clear as the water from my end. – MazeStricks May 30 '18 at 13:28
1

I solved it by creating a permanent redirect in Marketing -> URL Rewrites from contact/index to my custom URL. It works and even shows the successful message.

| improve this answer | | | | |
-1

Make the form asynchronous and ignore the default action: (example in JQuery because it's bundled with M2.)

$('.form.contact').on('submit',function(e){
  e.preventDefault();

  $.ajax({
    url:"<?php /* @escapeNotVerified */ echo $block->getFormAction(); ?>",
    data: $(this).serialize(),
    success: function(){ // it worked },
    error: function(){ // didn't work, why didn't he test his example before posting on SO?? }
  });

});
| improve this answer | | | | |

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.