OpenCart 3.0.0限制访问者的产品信息页面访问并要求注册?

时间:2017-07-24 08:37:21

标签: opencart opencart-module opencart2.3 opencart-3

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

到目前为止,我发现了这一点 - 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?,但我不确定它是否会起作用

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

1 个答案:

答案 0 :(得分:0)

catalog\controller\common\header.php添加:

public 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缓存等......)