网站首页 / 资讯 / Magento文档

如何在Magento的template页面添加登录链接点击后并返回当前页面

作者:admin

想必大家在做magento开发中肯定会遇到登录后返回前一个页面的情况,就比如在做magento页面或者是活动页面的时候,我们往往在活动页面展开活动,让用户必须登录后才能参加我们的活动,这样就需要登录了,但是magento登录后默认是进入用户账户中心的,这并不是我们需要的,我们需要用户在活动页面登录后还返回到他点击登录的活动页面这样,用户就可以参加活动了,用户体验更加友好。那么如何做呢?接下来我将给大家讲下。

当前magento有两个大的版本,一个是Magento 1一个是Magento 2那么我都会给大家讲解下。

首先Magento1的php代码如下:

<a id="r_login_customer" href="<?php
$url=base64_encode($refUrl);
Mage::getSingleton('customer/session')->setAfterAuthUrl(base64_decode($url));
echo Mage::getUrl('customer/account/login/',array('redirect'=>$url))?>">
<?php echo $this->__('Please <span style="color: #00a0e9;font-weight: bolder;">log in</span>')?>
</a>



其中$refUrl是你要跳转的页面,也即是点击登录的那个页面或者引用页面链接。

我们让引用页面的url进行了64位的编码计算,这样保证的安全和美观。

首先Magento2的php代码如下:

<?php
/**
* @WEBSITE https://www.magentola.com
*/
namespace Magentola\Hello\Controller\Test;
use Magentola\Hello\Helper\Data;
class Indexextends\Magento\Framework\App\Action\Action
{
protected $_coreRegistry;
protected $_pageFactory;
protected $_helper;
protected $_dir;
protected $_storeManager;
public function__construct(
\Magento\Framework\App\Action\Context$context,
\Magento\Framework\Registry$registry,
\Magento\Store\Model\StoreManagerInterface$storeManager,
Data $helper,
\Magento\Framework\View\Result\PageFactory$pageFactory)
{
$this->_pageFactory=$pageFactory;
$this->_coreRegistry=$registry;
$this->_storeManager=$storeManager;
$this->_helper=$helper;
return parent::__construct($context);
}
public functionexecute()
{
// echo  $this->_helper->getStoreCode();
//正确的代码如下
echo '<a href='.$this->_storeManager->getStore()->getUrl('customer/account/login',['referer'=>base64_encode('http://www.magentola.com/***.html')]).'>登录</a>';
}
}


通过以上的代码讲解希望对你有帮助


标签:
上一篇:M1与M2获取collection集合对象
下一篇:Magento获取产品分类的图片

相关内容

最近更新
相关产品
综合服务邮箱: magento2#foxmail.com