0

I have found a custom block that checks if the customer is logged in and edited it to this. What I need it to do is check the custom customer attribute is_allowed is set to yes, too. I want to use this in a block and call in it in phtml file. Can you point me where I am going wrong?

the code is

<?php

namespace WorldSynergy\LoginPortal\Block;

use Magento\Customer\Model\Url;
use Magento\Framework\App\Http\Context;
use Magento\Framework\View\Element\Template;
use Magento\Sales\Model\ResourceModel\Order\Customer\Collection;
{

class Portal extends Template
{
    protected $_customerUrl;
    protected $_customer;
    protected $httpContext;
    /**
     * Constructor
     *
     * @param \Magento\Framework\View\Element\Template\Context  $context
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Sales\Model\ResourceModel\Order\Customer\Collection $customer,
        Url $customerUrl,
        Context $httpContext,
        array $data = []
    ) {
        $this->_customer->$customer;
        $this->_customerUrl = $customerUrl;
        $this->httpContext = $httpContext;

        parent::__construct($context, $data);
    }

    /**
     * @return string
     */
    public function isLoggedIn()
    {
        if  $this->$_customer->create()->addAttributeToSelect('is_allowed') {
            return $this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
        }
    }
}

I can't seem figure it out. Help is appreciated. Thanks

0

2 Answers 2

0

Replace isLoggedIn() Funtion With This And Try

public function isLoggedIn()
    {
        if  ($this->$_customer->create()->addAttributeToSelect('is_allowed',array('eq'=>1))) {
            return $this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
        }
    }
2
  • thanks! I think this solved it
    – WS_Marc
    Sep 6, 2019 at 13:08
  • most welcome happy coding Sep 6, 2019 at 13:09
0

You have typo I guess, $customer remove $ symbol.

<?php

namespace WorldSynergy\LoginPortal\Block;

use Magento\Customer\Model\Url;
use Magento\Framework\App\Http\Context;
use Magento\Framework\View\Element\Template;
use Magento\Sales\Model\ResourceModel\Order\Customer\Collection;
{

class Portal extends Template
{
    protected $_customerUrl;
    protected $_customer;
    protected $httpContext;
    /**
     * Constructor
     *
     * @param \Magento\Framework\View\Element\Template\Context  $context
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Sales\Model\ResourceModel\Order\Customer\Collection $customer,
        Url $customerUrl,
        Context $httpContext,
        array $data = []
    ) {
        $this->_customer->$customer;
        $this->_customerUrl = $customerUrl;
        $this->httpContext = $httpContext;

        parent::__construct($context, $data);
    }

    /**
     * @return string
     */
    public function isLoggedIn()
    {
        if $this->_customer->create()->addAttributeToSelect('is_allowed',array('eq'=>1))) {
            return $this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
        }
    }
}
2
  • thanks! the typo was $this->_customer->$customer; should be $this->_customer = $customer;
    – WS_Marc
    Sep 6, 2019 at 13:07
  • yeah i have mentioned in answer..
    – aravind
    Sep 6, 2019 at 13:09

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.