0

i try to check if the user logged in , and i logged in but it returns the message says that i'm not logged in why ?? i tried 2 methods

<?php

namespace Main\Store\Block;

use Magento\Customer\Model\Session;

class wishlist extends \Magento\Framework\View\Element\Template
{
    protected $customerSession;

    /**
     * 
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        Session $customerSession,
        array $data = []
    ) {
        $this->customerSession = $customerSession;
        parent::__construct($context, $data);
    }



    public function addedToWishlist()
    {
        if ($this->customerSession->isLoggedIn()) {
            echo '<h2> Yes he is loggin';
        } else {
            echo '<h2> No he is not loggin';
        }


        // $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        // $customerSession = $objectManager->get('Magento\Customer\Model\Session');
        // if ($customerSession->isLoggedIn()) {
        //     echo '<h2> Yes he is loggin';
        // } else {
        //     echo '<h2> No he is not loggin';
        // }
    }
}

and this phml file

<?php

$wishlist = $block->getLayout()->createBlock('Main\Store\Block\wishlist');
$wishlist->addedToWishlist();

enter image description here

1
  • your code seems correct, alternatively you can try factory method as well describe in this blog: blog.chapagain.com.np/…
    – Pawan
    Jul 24, 2023 at 5:25

2 Answers 2

1

The Magento\Customer\Model\Session class cannot be used to determine if a customer is logged in or not when the page cache (full_page) is enabled.

By default, Magento has already added a flag to check whether a customer is logged in or not in the cache context. This flag is added to \Magento\Customer\Model\App\Action\ContextPlugin::beforeExecute method:

/**
 * Set customer group and customer session id to HTTP context
 *
 * @param ActionInterface $subject
 * @return void
 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
 */
public function beforeExecute(ActionInterface $subject)
{
    $this->httpContext->setValue(
        Context::CONTEXT_GROUP,
        $this->customerSession->getCustomerGroupId(),
        GroupManagement::NOT_LOGGED_IN_ID
    );
    $this->httpContext->setValue(
        Context::CONTEXT_AUTH,
        $this->customerSession->isLoggedIn(),
        false
    );
}

So, you can inject the Magento\Framework\App\Http\Context class in your class to get that flag value. The code should look similar to this:

use Magento\Framework\App\Http\Context as HttpContext;
//...
public function __construct(
    //...
    HttpContext $httpContext
) {
    //...
    $this->httpContext = $httpContext;
}

private function isCustomerLoggedIn()
{
    return (bool)$this->httpContext->getValue(CustomerContext::CONTEXT_AUTH);
}

Apply to your code, it should look like the following code:

use Magento\Framework\App\Http\Context as HttpContext;
//...
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        HttpContext $httpContext,
        array $data = []
    ) {
        $this->httpContext = $httpContext;
        parent::__construct($context, $data);
    }



    public function addedToWishlist()
    {
        if ((bool)$this->httpContext->getValue(CustomerContext::CONTEXT_AUTH)) {
            echo '<h2> Yes he is loggin';
        } else {
            echo '<h2> No he is not loggin';
        }
    }
}
1

There is a github issue for your problem. Its because of cache you got null in Magento\Customer\Model\Session


https://github.com/magento/magento2/issues/3294

Its safe to use factory methods. If you use factory you can get customer is logged in or not . You can use like below.

use Magento\Customer\Model\SessionFactory;

public function __construct(
        SessionFactory $customerFactory
    ) {
        $this->customerFactory = $customerFactory;
    }

In your method you can use

 $customer = $this->customerFactory->create();
        if ($customer->isLoggedIn()) {
//your code.
    }

Please upvote if its helpful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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