0

I am trying to create a simple module based on this answer https://magento.stackexchange.com/a/125519/45358

I just need a function to check if customer is logged or not in a custom theme header.phtml. I am new to Magento 2 and I can't understand how this works. I tried by following some tutorials but I am getting a lot of errors, like class not found and many more. I just need a complete example and then I will understand how actually work.

Here all files in my module

app/code/George/Core

registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
  \Magento\Framework\Component\ComponentRegistrar::MODULE,
  'George_Core',
  __DIR__
 );

Block/Userlogin/Login

<?php
namespace George\Core\Block\Userlogin;

class Login extends Magento\Framework\View\Element\Template
{
    public function isCustomerLoggedIn()
{
   return $this->_session->isLoggedIn();
  }

}

Model/Index/Index.php

<?php
namespace George\Core\Model\Index;

class Index extends \Magento\Framework\App\Action\Action
{
   protected $_session;

public function __construct(
\Magento\Customer\Model\Session $session,

) {
$this->_session = $session; 
  }

}

etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="George_Core" setup_version="0.0.1"/>
</config>

Also I found that need to add somewhere a preference, I understand that preference should be inside /etc/di.xml but how the actual di.xml file should look?

 <preference for="Block\That\Renders\The\Template"
        type="Vendor\Module\Block\Your\Custom\Block" />

Also in my case I can't understand which is Block\That\Renders\The\Template and Vendor\Module\Block\Your\Custom\Block

Now how can I call this inside custom theme header.phtml template, because if I try that, website stops loading.

if ($block->isCustomerLoggedIn()) {
// Customer is logged in
} else {
// Customer is not logged in
}

If someone can provide me a simple complete example will be very helpful.

George George
  • 319
  • 1
  • 3
  • 17

1 Answers1

0

Your di.xml should look like this ;

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
 <preference for="Block\That\Renders\The\Template"
        type="Vendor\Module\Block\Your\Custom\Block" />
</config>

Also Block\That\Renders\The\Template refers to the path of the block that renders the module. You can know the path of the block that renders the module by its corresponding xml. Lets take example of store_switcher block ,since it is located in all pages its xml is default.xml located in app/design/frontend/Magento/luma/Magento_Theme/layout/default.xml for luma theme .

Its reference container looks like ..

<referenceContainer name="footer">
     <block class="Magento\Store\Block\Switcher" name="store_switcher" as="store_switcher" after="footer_links" template="switch/stores.phtml"/>
    </referenceContainer>

So its corresponding phtml is switch/stores.phtml and block that renders it is Magento\Store\Block\Switcher

Vivek Kumar
  • 5,115
  • 2
  • 24
  • 50