show a custom attribute instead of full name if customer logged in M2
문제
I want to display in top menu my custom attribute instead of full name customer, I tried to override customer.phtml
under design my theme but no result please any help
<?
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerRepositoryInterface = $objectManager->create('\Magento\Customer\Api\CustomerRepositoryInterface');
$customerSession = $objectManager->create('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) {
$customerId = $customerSession->getCustomer()->getId();
$customeratt = $customerRepositoryInterface->getById($customerId);
$customAttributeCode = 'custom_name';
echo $cattrValue = $customeratt->getCustomAttribute($customAttributeCode); //Please echo this line when you want display custom name.
}
?>
now i can't find my top menu here is the code of default.xml under my theme `
and here is the code of my default.phtml under customer_module under my theme
<referenceBlock name="customer-account-navigation-billing-agreements-link" remove="true"/>
<!-- Product review link -->
<referenceBlock name="customer-account-navigation-product-reviews-link" remove="true"/>
<!-- My credit card link -->
<referenceBlock name="customer-account-navigation-my-credit-cards-link" remove="true"/>
<!-- Account link -->
<referenceBlock name="customer-account-navigation-account-link" remove="true"/>
<!-- Account edit link -->
<referenceBlock name="customer-account-navigation-account-edit-link" remove="true"/>
<!-- Orders link -->
<!-- <referenceBlock name="customer-account-navigation-orders-link" remove="true"/>
-->
<!-- Wish list link -->
<referenceBlock name="customer-account-navigation-wish-list-link" remove="true"/>
<referenceBlock name="top.links">
<referenceBlock name="register-link" remove="true" /> <!--for Create Account Link-->
<referenceBlock name="authorization-link" remove="true" /> <!--for Sign In Link -->
<referenceBlock name="wish-list-link" remove="true" /> <!--for WishList Link-->
</referenceBlock>
<referenceBlock name="header">
<arguments>
<argument name="show_part" xsi:type="string">other</argument>
</arguments>
</referenceBlock>
<move element="authorization-link" destination="header-wrapper" after="minicart"/>
<referenceBlock name="reviews.tab" remove="true" />
<referenceBlock name="product.review.form" remove="true" />
<referenceBlock name="product.info.review" remove="true" />
<referenceBlock name="product.info.details" remove="true"></referenceBlock>
<referenceBlock name="catalog.compare.sidebar" remove="true"/>
<referenceBlock name="view.addto.compare" remove="true" />
<referenceBlock name="view.addto.wishlist" remove="true" />
<?xml version="1.0"?>
<block class="Magento\Customer\Block\Account\AuthorizationLink" name="authorization-link-login" template="account/link/authorization.phtml"/>
</referenceBlock>
<block class="Magento\Theme\Block\Html\Header" name="header" as="header">
<arguments>
<argument name="show_part" xsi:type="string">welcome</argument>
</arguments>
</block>
<move element="header" destination="header.links" before="-"/>
<move element="register-link" destination="header.links"/>
<move element="top.links" destination="customer"/>
<move element="authorization-link" destination="top.links" after="-"/>
</body>
해결책
try this below code
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) {
$customerId = $customerSession->getCustomer()->getId();
$customerRepository = $objectManager->get('Magento\Customer\Api\CustomerRepositoryInterface');
$customer = $customerRepository->getById($customerId);
$cattrValue = $customer->getCustomAttribute('my_custom_attribute')->getValue();
}
?>
제휴하지 않습니다 magento.stackexchange