1

When we entering contact form in footer from any page it redirect only - /contact/index/ Any options such that we can submit form in footer section not redirect /contact/index

enter image description here

After submit form it show - enter image description here

I add contact form in footer section using this link - Magento2 contact form in CMS page, how to submit form without redirection and contact details not show in above footer section .Please reply me

1

I assume that you want to redirect back to the page, where the customer was before sending the contact request.

To do so, you need to create a simple module. Please follow the steps below to accomplish this.

Step 1: Create the registration.php file app/code/Stack/ContactRedirect folder and paste the following content in it:

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Stack_ContactRedirect',
    __DIR__
);

Step 2: Create the module.xml file under app/code/Stack/ContactRedirect/etc folder with the following content:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Stack_ContactRedirect" setup_version="0.0.1"/>
</config>

Step 3: Define 2 plugins under app/code/Stack/ContactRedirect/etc/frontend/di.xml file:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Contact\Controller\Index\Post">
        <plugin name="contactus_before_submit" type="Stack\ContactRedirect\Plugin\BeforePost"/>
    </type>
    <type name="Magento\Contact\Controller\Index\Post">
        <plugin name="contactus_submit" type="Stack\ContactRedirect\Plugin\AfterPost"/>
    </type>
</config>

Step 4: Create before plugin class under app/code/Stack/ContactRedirect/Plugin/BeforePost.php with the following content:

<?php
namespace Stack\ContactRedirect\Plugin;

class BeforePost {
    protected $_redirect;
    protected $_coreSession;

    public function __construct(
    \Magento\Framework\App\Response\RedirectInterface $redirect, \Magento\Framework\Session\SessionManagerInterface $coreSession
    ) {
        $this->_redirect = $redirect;
        $this->_coreSession = $coreSession;
    }

    public function beforeExecute(\Magento\Contact\Controller\Index\Post $subject) {
        $this->_coreSession->start();
        $this->_coreSession->setRerererUrl($this->_redirect->getRefererUrl());
        return;
    }
}

Step 5: Create after plugin class under app/code/Stack/ContactRedirect/Plugin/AfterPost.php with the following content:

<?php
namespace Mohit\ContactRedirect\Plugin;

class AfterPost {
    protected $_coreSession;

    public function __construct(
    \Magento\Framework\Session\SessionManagerInterface $coreSession
    ) {
        $this->_coreSession = $coreSession;
    }

    public function afterExecute(\Magento\Contact\Controller\Index\Post $subject, $result) {
        $this->_coreSession->start();
        return $result->setPath($this->_coreSession->getRerererUrl());
    }
}

Step 6: Your module is complete now. Run setup upgrade and then flush Magento's cache to see this module in action.

Note: There was a need to create before plugin also because in after plugin, there is no way to get the referral URL.

0

There are multiple ways to do this. You can create a after plugin of controller like below.

di.xml under etc folder

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Contact\Controller\Index\Post">
        <plugin name="contactus_submit" type="Vendor\Module\Plugin\Post"/>
    </type>
</config>

Post.php

<?php

namespace Vendor\Module\Plugin;

class Post
{
    /**
     * After plugin for execute method of \Magento\Contact\Controller\Index\Post
     *
     * @param \Magento\Contact\Controller\Index\Post $subject, $result
     * @return Redirect
     */
    public function afterExecute(\Magento\Contact\Controller\Index\Post $subject, $result)
    {
        return $result->setPath('custom-url');
    }
}

Let me know if you need more help on this.

  • How to create plugin for contact form - please explain full steps of controller – gaganbafna Nov 12 '18 at 12:14
  • @gaganbafna I have already write down the all steps. please try this and let me know if you face any issue while doing this. – Ramkishan Suthar Nov 12 '18 at 12:16
  • did it work for you? let me know if any help needed – Ramkishan Suthar Nov 12 '18 at 12:45
  • I used custom theme in magento , the path of di.xml file is app\etc and what is path of post.php – gaganbafna Nov 12 '18 at 12:52
  • You need to create a custom module for this. You can check this url to create your custom module devdocs.magento.com/videos/fundamentals/create-a-new-module – Ramkishan Suthar Nov 12 '18 at 12:56

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.