0

I want to restrict website visitors' access to category page only, And show a notification that visitors need to sign up to view individual product page. Will editing code in the controller/common header.php do? or do I need to add condition elsewhere? Any help would be appreciated.

So far I've found this - In Opencart, is there a way to restrict access to a page so that only people who are logged in and in a certain group can see the page? but I'm not sure whether it will work or not

EDIT: Ok, I've found there's usergroup in the dashboard. Is there anyway I can assign visitors to any user group and restrict their access to product page?

Vy Do
  • 46,709
  • 59
  • 215
  • 313

1 Answers1

0

Try this, in catalog\controller\common\header.php after public function index() { add:

// Where all visitors can access
$allowed_routes = array(
    'product/category',
    'common/home',
    'account/register',
    'account/login',
    'account/logout',
    'account/forgotten',
);

// Who can access
$allowed_customer_group_id = array(
    1,
    2,
    3
);

$current_customer_group_id = $this->config->get('config_customer_group_id');
if($this->request->get['route']){
    $current_page = $this->request->get['route'];
} else {
    $current_page = 'common/home';
}

// notification
$data['notice'] = '';

if(!in_array($current_customer_group_id, $allowed_customer_group_id) || !$this->customer->isLogged()){
    if(!in_array($current_page, $allowed_routes)){
        // not allowed
        // do something, redirect to login page
        if($this->customer->isLogged()){
            $this->response->redirect($this->url->link('common/home', '', true));
        } else {
            $this->response->redirect($this->url->link('account/login', '', true));
            // or $this->response->redirect($this->url->link('account/register', '', true));
        }
    } else {
        $data['notice'] = 'Some notification message here!';
    }
}

and somewhere in catalog\view\theme\default\template\common\header.twig add:

{% if notice %}
    <p class="alert alert-info">{{ notice }}</p>
{% endif %}

Finally you may need to clear caches (twig cache, ocmod cache, vqmod cache etc ...)

DigitCart
  • 2,980
  • 2
  • 18
  • 28