gpt4 book ai didi

Magento 如何在注册后重定向到主页

转载 作者:行者123 更新时间:2023-12-01 15:01:34 27 4
gpt4 key购买 nike

我正在使用 magento 创建一个电子商务网站。目前,如果新用户注册,网址将重定向到我的帐户页面。我想在新用户注册我的网站后显示主页。

指导我这样做。

提前致谢

最佳答案

虽然这是一篇旧帖子,但我觉得有必要在这里提供一个新答案,因为许多其他答案公然不遵循 Magento 最佳实践(例如:直接修改核心文件)或(虽然有效)他们不必要地重写客户帐户 Controller 。

下面的解决方案完全是通过使用观察者类来执行的。以这种方式这样做将确保您的逻辑不会因升级 Magento 而丢失(如果您直接修改了核心类文件),Magento 的核心开发团队也应该选择调整 Mage_Customer_AccountController::createPostAction 方法那些当您升级您的站点时,将带来更改(这不会让您重写 Controller 类文件)。

在下面的步骤中,根据需要创建任何目录以创建提到的文件。

第 1 步:定义您的模块

应用程序/etc/modules/Stackoverflow_Question10470629.xml

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

第 2 步:定义模块配置

app/code/local/Stackoverflow/Question10470629/etc/config.xml

<?xml version="1.0"?>
<config>
<modules>
<Stackoverflow_Question10470629>
<version>1.0.0</version>
</Stackoverflow_Question10470629>
</modules>
<global>
<models>
<stackoverflow_question10470629>
<class>Stackoverflow_Question10470629_Model</class>
</stackoverflow_question10470629>
</models>
</global>
<frontend>
<events>
<controller_action_postdispatch_customer_account_createPost>
<observers>
<stackoverflow_question10470629>
<type>singleton</type>
<class>stackoverflow_question10470629/observer_frontend</class>
<method>controllerActionPostdispatchCreatePostAction</method>
</stackoverflow_question10470629>
</observers>
</controller_action_postdispatch_customer_account_createPost>
<customer_register_success>
<observers>
<stackoverflow_question10470629>
<type>singleton</type>
<class>stackoverflow_question10470629/observer_frontend</class>
<method>customerRegisterSuccess</method>
</stackoverflow_question10470629>
</observers>
</customer_register_success>
</events>
</frontend>
</config>

第 3 步:定义您的观察者类

app/code/local/Stackoverflow/Question10470629/Model/Observer/Frontend.php

<?php
/**
* All publicly accessible method names correspond to the event they observe.
*
* @category Stackoverflow
* @package Stackoverflow_Question10470629
*/
class Stackoverflow_Question10470629_Model_Observer_Frontend
{

/**
* @param Varien_Event_Observer $observer
*/
public function customerRegisterSuccess($observer)
{
/* @var $session Mage_Customer_Model_Session */
$session = Mage::getSingleton('customer/session');

// This event occurs within Mage_Customer_AccountController::createPostAction
// however it occurs before the controller sets it's own redirect settings.
// Therefore we set this flag to true now, and then within the postdispatch
// we'll redirect to our custom URL
$session->setData('customer_register_success', true);
}

/**
* @param Varien_Event_Observer $observer
*/
public function controllerActionPostdispatchCreatePostAction($observer)
{
/* @var $controller Mage_Customer_AccountController */
/* @var $session Mage_Customer_Model_Session */

$session = Mage::getSingleton('customer/session');

// We must test for a successful customer registration because they
// could have failed registration despite reaching postdispatch
// (for example: they used an existing email address)
if ($session->getData('customer_register_success')) {
$session->unsetData('customer_register_success');

$url = Mage::getBaseUrl();
$controller = $observer->getData('controller_action');
$controller->getResponse()->setRedirect($url);
}
}

}

关于Magento 如何在注册后重定向到主页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10470629/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com