1 xml
<config>
<preference for="Magento\Customer\CustomerData\Customer" type="Expressproj\Rewrite\CustomerData\Customer"/>
</config>
2 php
<?php
/**
* Created by PhpStorm.
* User: admin
* Date: 2020/7/16 0016
* Time: 17:07
*/
namespace Expressproj\Rewrite\CustomerData;
use Magento\Customer\Helper\Session\CurrentCustomer;
use Magento\Customer\Helper\View;
/**
* Customer section
*/
class Customer implements \Magento\Customer\CustomerData\SectionSourceInterface
{
/**
* @var CurrentCustomer
*/
protected $currentCustomer;
/**
* @var View
*/
private $customerViewHelper;
/**
* @var \Magento\Customer\Api\GroupRepositoryInterface
*/
protected $_groupRepository;
/**
* @param CurrentCustomer $currentCustomer
* @param View $customerViewHelper
* @param \Magento\Customer\Api\GroupRepositoryInterface $groupRepository
*/
public function __construct(
CurrentCustomer $currentCustomer,
View $customerViewHelper,
\Magento\Customer\Api\GroupRepositoryInterface $groupRepository
) {
$this->currentCustomer = $currentCustomer;
$this->customerViewHelper = $customerViewHelper;
$this->_groupRepository = $groupRepository;
}
/**
* {@inheritdoc}
*/
public function getSectionData()
{
if (!$this->currentCustomer->getCustomerId()) {
return [];
}
$customer = $this->currentCustomer->getCustomer();
return [
'fullname' => $this->customerViewHelper->getCustomerName($customer),
'firstname' => $customer->getFirstname(),
'websiteId' => $customer->getWebsiteId(),
'groupId' => $customer->getGroupId(),
'groupCode' => $this->_groupRepository->getById($customer->getGroupId())->getCode(),
];
}
}
3 phtml
require([
'jquery',
'Magento_Customer/js/customer-data'
], function ($, customerData) {
let _interval = setInterval(function(){
let customer = customerData.get('customer');
if(customer().data_id){
console.log(customer())
if(customer().fullname){
$('.header .my-account').text(customer().fullname + ' (' + customer().groupCode + ')');
}
clearInterval(_interval);
}
},500);
});