在Magento中,提交联系表单后重定向到特定页面可以通过以下方法实现:
首先,需要创建一个自定义模块,以便在其中添加自定义代码。在Magento项目的app/code
目录下创建一个新的文件夹,例如MyCompany/ContactFormRedirect
,并在其中创建etc
文件夹。在etc
文件夹中创建module.xml
文件,其中包含以下内容:
<?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="MyCompany_ContactFormRedirect" setup_version="1.0.0">
<sequence>
<module name="Magento_Contact"/>
</sequence>
</module>
</config>
在自定义模块中创建一个控制器,用于处理联系表单提交并重定向到特定页面。在app/code/MyCompany/ContactFormRedirect/Controller
目录下创建ContactForm.php
文件,其中包含以下内容:
<?php
namespace MyCompany\ContactFormRedirect\Controller;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Action\Action;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\RequestInterface;
use Magento\Contact\Model\ContactFactory;
use Magento\Contact\Model\ConfigInterface;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\Translate\Inline\StateInterface;
use Magento\Framework\App\Action\HttpPostActionInterface;
class ContactForm extends Action implements HttpPostActionInterface
{
protected $_contactFactory;
protected $_config;
protected $_transportBuilder;
protected $_inlineTranslation;
protected $_pageFactory;
public function __construct(
Context $context,
ContactFactory $contactFactory,
ConfigInterface $config,
TransportBuilder $transportBuilder,
StateInterface $inlineTranslation,
PageFactory $pageFactory
) {
$this->_contactFactory = $contactFactory;
$this->_config = $config;
$this->_transportBuilder = $transportBuilder;
$this->_inlineTranslation = $inlineTranslation;
$this->_pageFactory = $pageFactory;
parent::__construct($context);
}
public function execute()
{
$post = $this->getRequest()->getPostValue();
if ($post) {
$this->_inlineTranslation->suspend();
$contact = $this->_contactFactory->create();
$contact->setData($post);
$errors = $contact->validateData();
$this->_inlineTranslation->resume();
if (!count($errors)) {
$contact->send();
$this->messageManager->addSuccessMessage(__('Your inquiry was submitted and will be responded to as soon as possible. Thank you for contacting us.'));
return $this->_redirect('contact/index', ['_fragment' => 'form']);
} else {
foreach ($errors as $error) {
$this->messageManager->addErrorMessage($error);
}
}
}
$resultPage = $this->_pageFactory->create();
return $resultPage;
}
}
在自定义模块中创建一个路由配置文件,以便将联系表单提交请求路由到自定义控制器。在app/code/MyCompany/ContactFormRedirect/etc
目录下创建frontend
文件夹,并在其中创建routes.xml
文件,其中包含以下内容:
<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="contactformredirect" frontName="contactformredirect">
<module name="MyCompany_ContactFormRedirect" />
</route>
</router>
</config>
最后,需要更新联系表单的action
属性,以便将表单提交请求路由到自定义控制器。在app/design/frontend/<Vendor>/<Theme>/Magento_Contact/templates/form.phtml
文件中找到表单元素,并将其action
属性更改为:
<form action="<?php echo $block->getUrl('contactformredirect/contactform/index') ?>" id="contactForm" method="post">
现在,当用户提交联系表单时,他们将被重定向到特定页面,而不是默认的联系页面。可以通过在ContactForm
控制器中自定义_redirect
方法来更改重定向页面。
领取专属 10元无门槛券
手把手带您无忧上云