Display information page to only logged in and particular customer group

611 views Asked by At

I found this following code on stackoverflow which gives access to information page to only logged in customer, and it is very useful. I want one enhancement to this. The information page should be only accessible by particular customer group.

if (isset($this->request->get['information_id']) && $this->request->get['information_id'] == '{ID}') 
{
    //If the information_id is provided and it matches the ID you wish to protect
    if (!$this->customer->isLogged()) {
        //If the customer is not logged in already, redirect them to the login page
        //Use $this->session->data['redirect'] to redirect them back to this page after logging in
        $this->session->data['redirect'] = $this->url->link('information/information', 'information_id=' . $this->request->get['information_id']);
        //Do the redirect
        $this->redirect($this->url->link('account/login', '', 'SSL'));
    }
}

Code Source: Is it possible to require a login for an OpenCart Information page, and ONLY the information page?

1

There are 1 answers

2
Sankar V On BEST ANSWER

Update your second if condition as shown below:

if (isset($this->request->get['information_id']) && $this->request->get['information_id'] == '{ID}') {

    if (!$this->customer->isLogged() || $this->customer->getCustomerGroupId()  != 1) { //where '1' is your customer groupid for which you want to give access.. 

        $this->session->data['redirect'] = $this->url->link('information/information', 'information_id=' . $this->request->get['information_id']);
        $this->redirect($this->url->link('account/login', '', 'SSL'));
    }
}

$this->customer->getCustomerGroupId() - returns your customer group id.

Have a nice day :) !!