3

I have added a login link in the footer by cms like as:

    <a href="{{store url="customer/account/login"}}">Login</a>

As soon as customer login I want to change that in the Logout link in magento2 footer.

enter image description here

2
  • Do you have access to theme xml file?
    – Gagan
    Aug 2, 2018 at 5:25
  • Yes i have access . Aug 2, 2018 at 5:41

3 Answers 3

4

Go to content -> Blocks -> "Footer Links Block"

Edit and add this link into content editor (below customer service link)

<li class="nav item">{{block class="CompanyName\ModuleName\Block\Footer" template="CompanyName_ModuleName::footer.phtml"}}</li>

and save it.

Create Footer.php block at CompanyName\ModuleName\Block\

<?php
namespace CompanyName\ModuleName\Block;

class Footer extends \Magento\Framework\View\Element\Template
{
    protected $_customerSession;
    protected $_storeManager;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        array $data = []
    ) {
        $this->_customerSession = $customerSession;
        $this->_storeManager = $storeManager;
        parent::__construct($context, $data);
    }

    /**
     * Check customer Login or not
     */
    public function checkCustomerLogin()
    {
        $customer_data = [];
        if (!$this->_customerSession->isLoggedIn()) {
            $customer_data['text'] = "Login";
            $customer_data['url'] = $this->_storeManager->getStore()->getUrl('customer/account/login');
        } else {
            $customer_data['text'] = "Logout";
            $customer_data['url'] = $this->_storeManager->getStore()->getUrl('customer/account/logout');
        }
        return $customer_data;
    }
}

Now, create footer.phtml file at /app/code/CompanyName/ModuleName/view/frontend/templates and add this code

<?php $customerData = $block->checkCustomerLogin(); ?>

<a href="<?php echo $customerData['url'];?>"><?php echo $customerData['text'];?></a>

Clean cache and check it.

6
  • I tried it but i am getting logout link in footer even i am not logged in Aug 2, 2018 at 6:47
  • can you update this if ($this->_customerSession->isLoggedIn()) to if (!$this->_customerSession->isLoggedIn()) in your answer ? Aug 2, 2018 at 6:52
  • Your answer is working best :) Aug 2, 2018 at 6:53
  • Can you update this @Rohan Then will accept this answer ? Aug 2, 2018 at 6:56
  • Oh Yes..!! It's by mistake. Thanks :). Now, Updated Aug 2, 2018 at 6:56
1

Using below code you can check if customer is logged in or not.

/** @var \Magento\Framework\App\ObjectManager $om */
$om = \Magento\Framework\App\ObjectManager::getInstance();
/** @var \Magento\Framework\App\Http\Context $context */
$context = $om->get('Magento\Framework\App\Http\Context');
/** @var bool $isLoggedIn */
$isLoggedIn = $context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);

So you can write code conditionally. If already logged in, show logout link otherwise login link.

You can call such phtml template in cms page as below

{{block class="Magento\Framework\View\Element\Template" template="[VendorName]_[ModuleName]::[YourTemplateFileLocation].phtml"}} 
7
  • I can use above code if i am using phtml file , how can i use that code in cms page admin ? Aug 2, 2018 at 5:01
  • I updated answer to call phtml in cms page Aug 2, 2018 at 5:04
  • I have tried by phtml but due to cache footer does not check dynamic conditions . Its need cache flush then its display effect. Aug 2, 2018 at 5:05
  • Use my given code. It will work also when cache is enabled. Aug 2, 2018 at 5:06
  • my project manager does not allowing me to use $om = \Magento\Framework\App\ObjectManager::getInstance(); in phtml file Aug 2, 2018 at 5:08
1
<referenceBlock name="footer_links">
    <block class="Magento\Customer\Block\Account\AuthorizationLink" name="authorization-link-login" template="account/link/authorization.phtml"/>
</referenceBlock>

This will place the same link in the footer which is on header. Put in your default.xml. This will place the link in the second column. I have not checked for First column.

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.