0

I have a block in a layout xml but I only want it to be displayed if the customer is not logged in. Is this possible?

  • Does your block have a phtml corresponding to it or its a cms block – Vivek Kumar Feb 19 '18 at 10:48
  • Its a cms block – Goose84 Feb 19 '18 at 10:48
1

There are a couple ways I can think to solve this. I am going to tell you how I would solve this issue but it may not suit your specific situation.

I would add a custom Layout Handle to not signed in customers, then add the block only in that layout.

This would allow me to add more blocks, customizations later to customers that are not signed in, additionally, it would allow me to more precisely tune my layout changes.

I would do an after plugin on whatever controller I needed the block to show up on in my example I am applying it everywhere:

\Magento\Framework\Controller\Index\Index::execute

Check if the customer is logged in by dependency injecting the following class to my Plugin controller:

\Magento\Customer\Model\Session $customerSession

Setting it as a property

$this->customerSession = $customerSession;

Adding that as a property to my plugin

protected $customerSession

And performing this check within afterExecute()

$this->customerSession->isLoggedIn();

The entire thing would look like this:

namespace YourCompany\YourModule\Plugin\Controller;

class LoggedIn
{
    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $customerSession;

    /**
     * @param \Magento\Customer\Model\Session $customerSession
     */
    public function __construct(
        \Magento\Customer\Model\Session $customerSession
    )
    {
        $this->customerSession = $customerSession;
    }

    /**
     * @param $object
     * @param $resultPage
     *
     * @return mixed
     */
    public function afterExecute($object, $resultPage)
    {
        if (!$this->customerSession->isLoggedIn()) {
            $resultPage->getLayout()->
            getUpdate()->
            addHandle('customer_not_logged_in');
        }

        return $resultPage;
    }
}

After that, you can make a layout file the normal way named:

customer_not_logged_in.xml Anything you change in this layout will only affect customers who are not logged in.

Caveats:

You may need to plugin a different controller. If for example, you want this to only apply to login pages you would instead of the CMS controller want to plugin afterExecute on the \Magento\Customer\Controller\Account\Login controller.

This is not proposed as the best solution but it is the solution I would/have used to solve this type of issue. Your mileage may vary.

I did not show how to create a plugin using di.xml the documentation on how to do that is here: http://devdocs.magento.com/guides/v2.2/extension-dev-guide/plugins.html

Good luck. & Have a good day.

| improve this answer | |
-1

You can use the methods, mentioned here: hide cms block on checkout page if customer logged in

Just change the code compatible with Magento 2

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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