to download samples the user should login or create an account first
-
15-04-2021 - |
Solution
To perform logic on the login status of a customer in a template use a helper class to detect if the customer is logged in
private $httpContext;
public function __construct(
\Magento\Framework\App\Http\Context $httpContext
) {
$this->httpContext = $httpContext;
}
/**
* is logged in using CONTEXT_AUTH
* @return boolean
*/
public function isLoggedIn()
{
return $this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);;
}
Depending on how you make your helper available to your template you can call the isLoggedIn() method and use it to display or hide content. Here I am using a ViewModel helper injected into the default Template block class.
$viewModel = $block->getViewModel();
?>
<ol>
<li><?php echo ($viewModel->isLoggedIn() ? 'LOGGED IN':'NOT LOGGED IN')?></li>
</ol>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange