0

My block function to get customer id not works

i'm printing customer details in my accounts custom tab

use Magento\Framework\View\Element\Template;
use Magento\Customer\Model\Session;


class myscheme extends \Magento\Framework\View\Element\Template
{



protected $customerSession;

public function __construct(
    Template\Context $context,
    \Magento\Customer\Model\Session $customerSession,
    array $data = []
) {
    $this->customerSession = $customerSession;
    parent::__construct($context, $data);
}


public function isCustomerLogged()
{
    if($this->customerSession->isLoggedIn())
        return "yes";
    else
        return "no";
}

public function getLoggedCustomerId(){
    return $this->customerSession->getCustomer()->getId();
}

Can anyone help me with this ?

1 Answer 1

0

Please try this

<?php
use Magento\Framework\View\Element\Template;
use Magento\Customer\Model\Session;
class myscheme extends \Magento\Framework\View\Element\Template
{
    protected $customerSession;
    protected $authContext;

    public function __construct(
        Template\Context $context,
        \Magento\Framework\App\Http\Context $authContext,
        \Magento\Customer\Model\Session $customerSession,
        array $data = []
    ){
        $this->customerSession = $customerSession;
        $this->authContext = $authContext;
        parent::__construct($context, $data);
    }


    public function isCustomerLogged()
    {
        $isLoggedIn = $this->authContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
        if($this->customerSession->isLoggedIn())
            return "yes";
        else
            return "no";
    }

    public function getLoggedCustomerId(){
        return $this->customerSession->getCustomer()->getId();
    }
}

Thanks

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.