方法一:
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
}
- 方法三:将客户会话类构造到您的相关块中,并创建一个获取客户名称的函数。然后从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>