This question already has an answer here:
I want to check that user is logged in or not in order to hide add to cart button for guest users. I have created a module in which I have override addtocart.phtml file and added the below code:
<?php $test = \Magento\Framework\App\ObjectManager::getInstance(); $context = $test->get('Magento\Framework\App\Http\Context'); $isLoggedInn = $context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH); ?> <?php if( $isLoggedInn ) : ?> <?php echo "text"; ?> <?php endif; ?>
It's working but is it a right approach because I have use object manager directly in phtml file.