Warning: Cannot modify header information - headers already sent by (output started at /www/wwwroot/sbboke.com/wp-content/plugins/wordpress-seo/src/integrations/front-end-integration.php:409) in /www/wwwroot/sbboke.com/wp-content/themes/pacify/content-single.php on line 5

Magento2 如何检查客户是否登录?

判断客户是否登录,这算是比较常用的代码了

比较简约和常见的方式如下:

通过以下代码,您可以在任何地方检查客户是否登录

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$customerSession = $objectManager->get('Magento\Customer\Model\Session'); 
if($customerSession->isLoggedIn()) { 
   // 客户登录操作
}

来自控制器

$customerSession = $this->_objectManager->get('Magento\Customer\Model\Session'); 
if($customerSession->isLoggedIn()) { 
   // 客户登录操作
}

比较完善点的如下:

在除模板之外的任何类中

您首先需要在构造函数中注入以下类/Magento/Customer/Model/Session::

protected $_customerSession;    // don't name this `$_session` since it is already used in \Magento\Customer\Model\Session and your override would cause problems

public function __construct(
    ...
    \Magento\Customer\Model\Session $session,
    ...
) {
    ...
    $this->_customerSession = $session;
    ...
}

然后在你的班级中你可以调用以下内容:

if ($this->_customerSession->isLoggedIn()) {
    // Customer is logged in 
} else {
    // Customer is not logged in
}

在模板中

它需要在模板中进行更多的工作,因为您必须为呈现模板的块设置首选项才能以干净的方式执行此操作:

<preference for="Block\That\Renders\The\Template"
            type="Vendor\Module\Block\Your\Custom\Block" />

然后,在您的自定义块构造函数中,您需要遵循与任何类相同的依赖项注入(如上所述)。

这里的额外步骤是创建一个公共方法,可在模板中使用该方法来检查客户是否登录

public function isCustomerLoggedIn()
{
    return $this->_customerSession->isLoggedIn();
}

然后在您的模板中您可以调用:

if ($block->isCustomerLoggedIn()) {
    // Customer is logged in
} else {
    // Customer is not logged in
}

上面的解决方案, 在生产模式下启用全页缓存和 Varnish 时,可能会出现无法使用的情况,这时候,要不在布局XML里使用
cachable="false"

要不就使用下面的解决方案:

<?php $_loggedin = $block->getLayout()->createBlock('Magento\Customer\Block\Account\AuthorizationLink')->isLoggedIn() ?>
 <?php if( $_loggedin ) : ?>
   // your code
 <?php endif; ?>

也可以使用下面的解决方案:

如果您想在启用 Magento 默认 FPC 缓存后使用它,则从 Session 模型获取记录的状态将不起作用,在这种情况下,您应该使用 SessionFactory。

如果启用 FPC 缓存,则不会启动会话,详细信息:https://github.com/magento/magento2/issues/3294#issuecomment-328464943

要解决这个问题,您必须使用SessionFactory,例如:

/**
* @var \Magento\Customer\Model\Session
*/
protected $_customerSessionFactory;

public function __construct(
    ....
    \Magento\Customer\Model\SessionFactory $customerSessionFactory
    ....
) 
{
    ....
    $this->_customerSessionFactory = $customerSessionFactory;
    ....
}

public function getCustomerId(){
  $customer = $this->_customerSessionFactory->create();
  echo $customer->getCustomer()->getId();
}

还有其他解决方案:

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Block\Account\AuthorizationLink');
if ($customerSession->isLoggedIn() == true) {
//your code.
} ?>

或者

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->create('Magento\Customer\Model\Session');

if ($customerSession->isLoggedIn()) {
    $customerSession->getCustomerId();  // get Customer Id
    $customerSession->getCustomerGroupId();
    $customerSession->getCustomer();
    $customerSession->getCustomerData();

    echo $customerSessionget->getCustomer()->getName();  // get  Full Name
    echo $customerSessionget->getCustomer()->getEmail(); // get Email
}

或者

 $om = \Magento\Framework\App\ObjectManager::getInstance();
 $context = $om->get('Magento\Framework\App\Http\Context');
 $isLoggedIn = $context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
 if($isLoggedIn){
      echo "Yes Customer loggedin";
      echo "<pre>";print_r($context->getData()); 
 }

或者

<script type="text/javascript">
    require(['jquery'], function($){
        jQuery(document).ready( function() {
            var isLoggedIn = jQuery('.authorization-link > a').attr('href').indexOf('/login')<0;
            
            if(isLoggedIn){
                
            }else{
                
            }
        });
    });
</script>

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
内联反馈
查看所有评论