Question

enter image description here

i want to restrict the user so that if he wants to download the sample data he would have to login or create an account first otherwise he would be not be able to download the sample data.

Was it helpful?

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
scroll top