Skip to content

how to check if the the user logged in?

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 MainStoreBlock;

use MagentoCustomerModelSession;

class wishlist extends MagentoFrameworkViewElementTemplate
{
    protected $customerSession;

    /**
     * 
     * @param MagentoFrameworkViewElementTemplateContext $context
     * @param array $data
     */
    public function __construct(
        MagentoFrameworkViewElementTemplateContext $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 = MagentoFrameworkAppObjectManager::getInstance();
        // $customerSession = $objectManager->get('MagentoCustomerModelSession');
        // 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('MainStoreBlockwishlist');
$wishlist->addedToWishlist();

enter image description here