0

我想限制网站访问者仅访问类别页面,并显示访问者需要注册才能查看单个产品页面的通知。将在控制器/通用 header.php 中编辑代码吗?还是我需要在其他地方添加条件?任何帮助,将不胜感激。

到目前为止,我发现了这个 -在 Opencart 中,有没有办法限制对页面的访问,以便只有登录并在某个组中的人才能看到该页面?但我不确定它是否会起作用

编辑:好的,我发现仪表板中有用户组。无论如何,我可以将访问者分配给任何用户组并限制他们对产品页面的访问吗?

4

1 回答 1

0

试试这个,在添加catalog\controller\common\header.phppublic function index() {

// 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!';
    }
}

并在某处catalog\view\theme\default\template\common\header.twig添加:

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

最后,您可能需要清除缓存(twig 缓存、ocmod 缓存、vqmod 缓存等...)

于 2017-11-24T16:51:16.520 回答