0

I have these codes for account link. How can I change the code in phtml account link section so that if user is not logged in the acount link section code will not execute else it will execute.

app/design/.... /account-links.phtml

<?php if ($accountLinksHtml): ?>
<div id="header-account" class="account-links top-links links-wrapper-separators-left skip-content skip-content--style">
    <?php echo $accountLinksHtml; ?>
</div>

app/code/...... /account-links.php

class AccountLinks extends \Magento\Framework\View\Element\Template
{
    /**
     * Theme helper
     *
     * @var HelperData
     */
    protected $theme;

    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param HelperData $helperData
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        HelperData $helperData,
        array $data = []
    ) {
        $this->theme = $helperData;
        parent::__construct($context, $data);
    }

   /**
     * Get helper
     *
     * @return HelperData
     */
    public function getHelperTheme()
    {
        return $this->theme;
    }

}

this is the error I am getting if I add the public function in Accountlinks.php

  • please explain briefly. – Rk Rathod Jul 23 at 12:49
  • I have designed a custom theme. In the header section it shows My Account in general. I want to remove that for guest user. In the phtml file under app/design I have this section of code which fetches the my account. I want to execute this only if user is logged in otherwise I dont want this section to execute. So how do I put a check condition before this that if logged in then execute else not – Naiwrita09 Jul 23 at 13:04
  • in your phtml you can get or check your customer is logged in or not ? – Rk Rathod Jul 23 at 13:11
  • please share screenshot to understand better. – Mohit Rane Jul 23 at 13:31
  • In your custom theme which phtml file use? Or what is the path of your phtml file? – Sweety Masmiya Jul 24 at 4:31
1

In your Block File

class AccountLinks extends \Magento\Framework\View\Element\Template
{
    /**
     * Theme helper
     *
     * @var HelperData
     */
    protected $theme;

    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param HelperData $helperData
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Model\Session $customerSession
        HelperData $helperData,
        array $data = []
    ) {
        $this->theme = $helperData;
        $this->customerSession = $customerSession;
        parent::__construct($context, $data);
    }

   /**
     * Get helper
     *
     * @return HelperData
     */
    public function getHelperTheme()
    {
        return $this->theme;
    }

    public function isLoggedIn()
    {
        return $this->customerSession->isLoggedIn();
    }

}

In your Phtml files

<?php if($block->isLoggedIn()) {
   // Logged in, You can make your changes here
} else {
   // Not logged in
} ?>

Hope it helps.

  • Do I have to call the block file from any layout file? – Naiwrita09 Jul 24 at 6:58
  • you can add this code to your block file (AccountLinks.php) – Mohit Rane Jul 24 at 7:01
  • ok. I will implement it. – Naiwrita09 Jul 24 at 7:35
  • but this class in AccountLinks.php is already extending some other class. Can you provide me the entire the file how it can be added in the AccountLinks.php – Naiwrita09 Jul 25 at 5:48
  • it's the same class \Magento\Framework\View\Element\Template. check updated answer – Mohit Rane Jul 25 at 5:59
0

you could use below code to check,

Use of objectmanager is not recommended

Try injecting customer session in block file and call block function from phtml

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) {
   // customer login action
}
  • "Try injecting customer session in block file and call block function from phtml" how this is done can you provide me a detailed solution. and the above section of code is added in the app/design section. – Naiwrita09 Jul 24 at 4:07
  • First check whether it is working or not by using objectmanger. It can be added in phtml itself. Add above code in your phtml – aravind Jul 24 at 4:26
  • I am editing my post and giving the details of both the files. please suggest me how it can be done. this method is not loading giving an error – Naiwrita09 Jul 24 at 4:31
  • using object manager can you provide the syntax. I am completely new and I do not know how to implement the nested if which contains an html in between. – Naiwrita09 Jul 24 at 4:39
  • what error you are getting? – aravind Jul 24 at 4:49
0

How do you inject the client session into the block file? Can you explain me better? I have a similar problem. I need to restrict all products with a restriction flag for the customer to be able to purchase it only if they are logged in.

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.