-1

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?

Community
  • 1
  • 1
Asif
  • 1
  • 3

1 Answers1

0

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 :) !!

Sankar V
  • 4,110
  • 5
  • 28
  • 52
  • Something going wrong. Redirection is ok but showing to all customer group. Help!!! – Asif Dec 14 '13 at 06:13
  • if (!$this->customer->isLogged() `||` $this->customer->getCustomerGroupId() != 1) { - updated.. please find my updated answer. earlier i used `AND` instead of `OR`. – Sankar V Dec 14 '13 at 06:49