Magento 2 登录时如何显示客户名称?

方法一:

1) link.php

2) authorization.phtml

I have such code in link.php:

if (false != $this->getTemplate()) {
     return parent::_toHtml();
     }
     return '<div><a ' . $this->getLinkAttributes() . ' >' . $this->escapeHtml($this->getLabel()) . '</a></div>';
    }

方法二:使用对象管理器到您的phtml文件中:

$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 $customerSession->getCustomer()->getName();  // get  Full Name
   echo $customerSession->getCustomer()->getEmail(); // get Email
}
  1. 方法三:将客户会话类构造到您的相关块中,并创建一个获取客户名称的函数。然后从phtml文件调用您的块函数。

方法四:

在 helper 文件中使用以下函数,Magento\Customer\Model\Session以在$ customerSession中获取会话对象

public function getLoggedinCustomerName(){

        if($customerSession->getData('customer_id'))
        {
            $customer = $this->_customerRepositoryInterface->getById($customerSession->getData('customer_id'));
            return $customer->getFirstname();
        }
        else
        {
            return "Guest";
        }
    }

并在您的PHTML文件中使用它

<?php
$customerName = $helper->getLoggedinCustomerName();
?>
 <a href="<?php echo $block->getUrl('customer/account/'); ?>">
                <i class="fa fa-user" aria-hidden="true"></i>
                <span class="label"><?php /* @escapeNotVerified */ echo 'Hello '. $customerName; ?></span>
            </a>

相关文章

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