I would like to remove My Account
link from top links, when customer is not logged in.
How do I achieve it via XML layout files?
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityYou can do it without xml file also which will be easier for your task. First step that you have to do is override header part and once its done apply some simple logic like by checking cx is logged in or not.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) {//apply logic here}
You can remove via overriding theme layout or xml file.
app/design/frontend/Package/themename/Magento_Customer/layout/default.xml
<referenceBlock name="top.links">
<referenceBlock name="register-link" remove="true" /> <!--for Create Account Link-->
<referenceBlock name="my-account-link" remove="true" /> <!--for My Account Link-->
</referenceBlock>
I hope this will help you
Okay, there are two ways to go about this, you can do it with XML but also at the same time you're able to also do this with a helper.
The First way through a helper requires modification of theme templates and also a module making for Magento.
So...
Create your module, call it what you will And add a helper file into
Vendor\Module\Helper\Data.php
Inside this file you can use the following:
namespace Vendor\Module\Helper;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
/**
* @var \Magento\Framework\App\Http\Context
*/
private $httpContext;
public function __construct(
\Magento\Framework\App\Helper\Context $context,
\Magento\Framework\App\Http\Context $httpContext
) {
parent::__construct($context);
$this->httpContext = $httpContext;
}
public function isLoggedIn()
{
$isLoggedIn = $this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
return $isLoggedIn;
}
}
This will then allow you in your frontend theme to use:
$helper = $this->helper('Vendor\Module\Helper\Data');
if($helper->isLoggedIn()):
...
echo 'Customer is logged in';
...
endif;
This won't be affected by full page cache and means you can add this when and where you need.
Second Option. The XML One.
Again you'll need to create a module for Magento, but this time you're going to create a module that adds XML handles to the page. This one is a little more complicated than using the helper.
So in this one, we're going to create an observer to add the handles to the page if the customer is either logged in or logged out.
Our first observer:
Vendor\Module\Observer\AddHandles.php
Will look like this:
namespace Vendor\Module\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Customer\Model\Session as CustomerSession;
class AddHandles implements ObserverInterface
{
/** @var CustomerSession */
protected $customerSession;
/**
* AddHandles constructor.
* @param CustomerSession $customerSession
*/
public function __construct(
CustomerSession $customerSession
) {
$this->customerSession = $customerSession;
}
/**
* Determine if the customer is logged in and if so add a layout handle
* @param Observer $observer
*/
public function execute(Observer $observer)
{
$layout = $observer->getEvent()->getLayout();
if ($this->customerSession->isLoggedIn()) {
$layout->getUpdate()->addHandle('logged__in');
} else {
$layout->getUpdate()->addHandle('logged__out');
}
}
}
Then inside of:
Vendor\Module\etc\frontend\events.xml
We'll add the following to call the handles for the frontend.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="layout_load_before">
<observer name="add_layout_handles" instance="Vendor\Module\Observer\AddHandles" />
</event>
You'll then be able to use the xml files:
logged__in.xml
logged__out.xml
In your frontend veiw.