How to check if user is logged in to twig file in Journal 3 in OpenCart 3.X?

510 Views Asked by At

If the user is not logged into their account, I need to display a login button. If he has already logged in, display the menu.

How to check if a user is logged in or not in a twig file?

1

There are 1 best solutions below

0
Baracsi Róbert On

You wrote nothing concrete, so I'll show in cart section to display a login button when user is logged out.

You have to handle it (logged in or logged out state) in controller file and examine it in twig file.

modify catalog/controller/checkout/cart.php

public function index() {
  $this->load->language('checkout/cart');

  $this->document->setTitle($this->language->get('heading_title'));

  $data['breadcrumbs'] = array();

  $data['breadcrumbs'][] = array(
    'href' => $this->url->link('common/home'),
    'text' => $this->language->get('text_home')
  );

  $data['breadcrumbs'][] = array(
    'href' => $this->url->link('checkout/cart'),
    'text' => $this->language->get('heading_title')
  );

  // this will be examined in twig file
  $data['my_isloggedout'] = !$this->customer->isLogged();
  $data['my_login_text'] = sprintf($this->language->get('text_login'), $this->url->link('account/login'), $this->url->link('account/register'));
  [...]
}

modify catalog/view/theme/journal3/template/checkout/cart.twig

  {% if my_isloggedout %}
    <button type="button" class="btn btn-default">{{ my_login_text }}</button>
  {% else %}
    <!-- display menu -->
  {% endif %}

After modification in files maybe you have to refresh modifications on admin site (Extensions \ Modifications \ Refresh button)