Question

When the cache is enabled customer name is not showing on the product page.

When the cache is disabled customer name is showing properly on the product page.

header.phtml

<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/**
 * @var \Magento\Theme\Block\Html\Header $block
 */

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

//$welcomeMessage = $block->getWelcome();
?>

<li><a href="<?php echo $block->getUrl('about-us'); ?>">About Us</a></li>
<li><a href="<?php echo $block->getUrl('contact-us'); ?>">Contact Us</a></li>
<li><a href="<?php echo $block->getUrl('sales/order/history'); ?>">My Orders</a></li>
<li><a href="<?php echo $block->getUrl('wishlist'); ?>">Wish List</a></li>
<li><a href="<?php echo $block->getUrl('ordertracking'); ?>">Track Order</a></li>

<?php echo $this->getLayout()
    ->createBlock('Codism\Csr\Block\Index\TopMenu')
    ->setTemplate('Codism_Csr::menu.phtml')
    ->toHtml();
?>

<?php if($customerSession->isLoggedIn()) {?>
<li>Hi, <?php echo $customerSession->getCustomer()->getName(); ?></li>
<?php }?>

Screenshot

enter image description here

Was it helpful?

Solution

You need to add this below line in your header.phtm file :

<span data-bind="text: customer().fullname ? $t('Welcome, %1!').replace('%1', customer().fullname) : '<?=$block->escapeHtml($welcomeMessage) ?>'"></span>

OTHER TIPS

Use below code.

<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/**
 * @var \Magento\Theme\Block\Html\Header $block
 */

$ObjectManager= \Magento\Framework\App\ObjectManager::getInstance();
$context = $ObjectManager->get('Magento\Framework\App\Http\Context');
$isLoggedIn = $context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
$customerSession = $ObjectManager->get('Magento\Customer\Model\Session');




//$welcomeMessage = $block->getWelcome();
?>

<li><a href="<?php echo $block->getUrl('about-us'); ?>">About Us</a></li>
<li><a href="<?php echo $block->getUrl('contact-us'); ?>">Contact Us</a></li>
<li><a href="<?php echo $block->getUrl('sales/order/history'); ?>">My Orders</a></li>
<li><a href="<?php echo $block->getUrl('wishlist'); ?>">Wish List</a></li>
<li><a href="<?php echo $block->getUrl('ordertracking'); ?>">Track Order</a></li>

<?php echo $this->getLayout()
    ->createBlock('Codism\Csr\Block\Index\TopMenu')
    ->setTemplate('Codism_Csr::menu.phtml')
    ->toHtml();
?>

<?php if($isLoggedIn) {?>
<li>Hi, <?php echo $customerSession->getCustomer()->getName(); ?></li>
<?php }?>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top