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.
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.
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.
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"}}
<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.