8

This question already has an answer here:

How can I check whether user is logged in or not in Magento 2.0 I using phtml file I want to check user logged in or not.

<span class="selllink switcher top-links">
    <span class="" data-bind="scope: 'customer'">
        <span data-bind="text: customer().fullname ? $t('Welcome,%1').replace('%1', customer().firstname) : '<?=$block->escapeHtml($welcomeMessage) ?>'"></span>
    </span>
    <script type="text/x-magento-init">
    {
        "*": {
            "Magento_Ui/js/core/app": {
                "components": {
                    "customer": {
                        "component": "Magento_Customer/js/view/customer"
                    }
                }
            }
        }
    }
    </script>
</span>

marked as duplicate by Keyul Shah, Murtuza Zabuawala, Manthan Dave, brentwpeterson, liyakat Apr 25 '17 at 3:09

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.

  • You mention "using phtml file". If you want to hack the code into any PHTML template and not use or create a block class that gives you access to this specific information, refer to one of the "Object Manager" answers of the linked duplicate. If you are willing to take the extra effort and go for a clean solution, please update your question with more information: which template, which block, how do you use it? – Fabian Schmengler Jul 8 '16 at 12:56
-4
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');


if($customerSession->isLoggedIn()) {
    // do your code
}
  • It's working but username not showing on aws. @RJ07 – vnnogile_user Jul 13 '16 at 9:45
  • Why this is marked as solution when it's not working. And again creating new instance of the object manager in template file. Check out my answer and the comments below. Other thing is that you are trying to display the username with knockoutJS variable which is out of the question scope. – Miroslav Petroff Jul 18 '16 at 11:37
17

To check if customer is loged in or not you can call Magento\Customer\Model\Session::isLoggedIn() method.

If you want to do that properly in *.phtml files you should use helper inside the template. Object manager is not preferred way to call models inside template files. So I am not recommending to follow this post There isn't core helper that can be reused so you need to create new one.

<?php
namespace YourCompany\ModuleName\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Customer\Model\Session $customerSession
    ) {
        $this->customerSession = $customerSession;
        parent::__construct($context);
    }

    public function isLoggedIn()
    {
        return $this->customerSession->isLoggedIn();
    }
}

Then in your *.phtml file you can call your helper and call isLoggedIn method like this:

<?php $helper = $this->helper('YourCompany\ModuleName\Helper\Data'); ?>
<?php if($helper->isLoggedIn()) : ?>
    logged in
<?php else : ?>
    not logged in
<?php endif; ?>
  • 1
    Yes It's Working. – vnnogile_user Jul 8 '16 at 12:40
  • When i deployed on AWS server it is not working. – vnnogile_user Jul 11 '16 at 6:59
  • 2
    What you mean by "not working" Be more specific. Do you have enabled caches? Try to set cacheable="false" for your block in the layout xml file – Miroslav Petroff Jul 12 '16 at 7:39
  • Yes it's working but username is not showing now. @Miroslav Petroff – vnnogile_user Jul 13 '16 at 6:54
  • 2
    This worked for me. The trick was to add cacheable="false" at the block level in layout XML. – thdoan Apr 24 '17 at 6:48
4

This is for only Magento 2.0, add this in phtml file:

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

Check via following:

if($customerSession->isLoggedIn()){
   // your code
}
1

If you want to get user logged in at template level phtml, you can call :

<?php $_loggedin = $this->helper('Magento\Checkout\Helper\Cart')->getCart()->getCustomerSession()->isLoggedIn(); ?>
0

You can also check with below code

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

    if($isLoggedIn){
        // Logged In
    }else{
        // Not Logged In
    }
  • That solution is working for me on all pages Thanks. – Nitesh May 19 '17 at 12:31
0

You must call object in magento 2 for custom coding in any page. so code will look like this

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

if($customerSession->isLoggedIn()){

//Code

}
0

I am try to get the logged in condition on header.phtml file with above helper code in custom module app/code/venter/ module name/ helper/Data.php Actually my concerns is thats i want to restrict all the categories products page cms page from the frontend if any frontend users are not logged in it will not access any pages except home page.where we apply the code.please anyone help me

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
public function __construct(
    \Magento\Framework\App\Helper\Context $context,
    \Magento\Customer\Model\Session $customerSession
) {
    $this->customerSession = $customerSession;
    parent::__construct($context);
}

public function isLoggedIn()
{
    return $this->customerSession->isLoggedIn();
}

}

Not the answer you're looking for? Browse other questions tagged or ask your own question.