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.

  • How are you adding the link to the samples download? – paj Mar 12 at 11:21
  • just by creating downloadable product in magento create product option and adding the file there – user3721386 Mar 13 at 5:23
  • you would need to override the template that loads the downloadable product link and then use the logic in my answer to display or hide the downloadable product. – paj Mar 13 at 10:32
  • can i prompt the user to login or create account first when the downloadable product is shown – user3721386 Mar 13 at 10:59

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();
    <li><?php echo ($viewModel->isLoggedIn() ? 'LOGGED IN':'NOT LOGGED IN')?></li>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.