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:


$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.

marked as duplicate by Manoj Deswal, Piyush, Jai, liyakat, Prince Patel Jan 30 '18 at 9:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    Using the object manager is a bad practice (except from a few exceptions but certainly not in this case). – André Ferraz Jan 30 '18 at 9:16
  • Actually I want to hide add to cart button for guest users – Syed Muneeb Ul Hasan Jan 30 '18 at 10:43

Browse other questions tagged or ask your own question.