Skip to main contentSkip to footer

How to check if Customer or Admin is logged in the front page of Opencart 2 and 3?

Rupak Nepali
Share:
How to check if Customer or Admin is logged in the front page of Opencart 2 and 3?

This is opencart tips and tricks to check if Customer or Admin is logged in the front page of Opencart 2.3 and Opencart version 3. You can force login to customer. To check admin you should log in the same browser.

Force login for customer

Open catalog/controller/common/header.php

Find the index method: public function index() { then paste the following lines of code:

if ($this->request->get['route'] != "account/login") {
    if (!$this->customer->isLogged()) {
        $this->session->data['redirect'] = $this->url->link('common/home', '', true);
        $this->response->redirect($this->url->link('account/login', '', true));
    }
}

The above code check if route is login page or not and if it is not then it checks whether the customer is logged in or not, if not logged in then it redirects to login page. If the customer is logged in then it acts as normally.

Check whether Admin is logged in or not from the catalog or frontend

Now go to any controller and insert the code to check if user_id is set in the session. For example: Go to catalog/controller/common/header.php

Inside the index() method add the following code:

if($this->session->data['user_id']){
 echo 'Admin is logged in';
}else{
 echo 'Admin is not logged in';
}

Another way initialize the user object and check with isLogged() method

$loggeduser = new Cart\User($this->registry);
if($loggeduser->isLogged()){
 echo "Admin is in";
}else{
 echo "Admin not in";
}

This is how we can use Opencart library global objects methods.

Let us know if you find any difficulties or need any help from us. Likewise, you can see other Opencart tips and tricks and Opencart tutorial

Comments

Join the conversation and share your thoughts

Leave a Comment

Your comment will be reviewed before publishing.

Be the first to comment on this post!

Innovation

Let's Make Something Amazing Together

We always provide our best creative ideas at the highest level. Tell us about your project and we will make it work.

InnovateBringing innovative solutions to complex problems
AutomateStreamlining processes through automation
DominateLeading the market with exceptional results
Build Smarter and Launch FasterEfficient development for rapid deployment