MattM
MattM

Reputation: 3217

Opecart - check if user in on a specific page (is_page command?)

I want to make sure only specific users can access my site content based on if they are logged in and part of a customer group. If not I kick them out of the wholesale site and into the regular site using this code.

if ($logged && !$this->customer->getCustomerGroupId() == '1') { /*Check if logged in and belong to wholesale group*/
        $this->customer->logout();
        $this->redirect("https://example.com/");
    }

The problem is that if they aren't logged in and find a way they can still browse the site. I would like a way to check if they are logged in. I tried using this:

if (!$logged) { /*Check if user is logged in, not = redirect to login page*/
    $this->redirect('https://wholesale.garrysun.com/index.php?route=account/login'); 

but then they get stuck in a redirect loop because the login page has this in the header. I would like to check for this on every page except for the login and logout pages:

http://example.com/index.php?route=account/login

http://example.com/index.php?route=account/logout

After thinking about it I tried using this code but to no avail:

<?php 
/*Check if on wholesale site*/
 if($this->config->get('config_store_id') == 1) {
    /*Check if user is logged in, if not and not on login/logout/register page = redirect to login page*/
    if(!$logged){
        if(!$this->request->get['route'] == 'account/login' || !$this->request->get['route'] == 'account/logout' || !$this->request->get['route'] == 'account/register'){ 
            $this->redirect('https://wholesale.garrysun.com/index.php?route=account/login'); 
        } 
    }else if($logged && !$this->customer->getCustomerGroupId() == '1') { /* User is logged in and not a wholesale customer */
        $this->customer->logout();
        $this->redirect("https://garrysun.com/");
    }
}
?>

What is the code in opencart to check if you are on a specific page?

Upvotes: 1

Views: 798

Answers (1)

rjdown
rjdown

Reputation: 9227

This information isn't passed to any of the controllers. The best you can do is pretty much what you already have:

if (isset($this->request->get['route'])) {
    $page = $this->request->get['route'];
} else {
    $page = 'common/home';
}

if ($this->config->get('config_store_id') == 1) {
    if (!$this->customer->isLogged() && !in_array($page, array('account/login', 'account/logout', 'account/register'))) {
        ... redirect
    }
}

Upvotes: 1

Related Questions