phpxmlopencart2.xvqmod

how to restrict access to logged in users only in opencart using vqmod?


Currently when I go to https://domain.com I can see the full front page and only when I click on some item, I am directed to the login page. I would like to be directed to the login page right after typing https://domain.com so that unregistered users can't see the products and prices on the front page.

Currently and using vQmod with xml file named force_customer_login.xml looks like:

<modification>

    <file name="catalog/controller/common/header.php">
        <operation>
            <search position="after"><![CDATA[
            function index()
            ]]></search>
            <add trim="true"><![CDATA[
            //Q: Force Customer Login
            $match = false;
            if (!empty($this->request->get['route'])) {

                $skip = array(
                    'payment',
                    'feed',
                    'forgotten',
                    'login',
                    'register',


                );

                foreach ($skip as $s) {
                    if (strpos($this->request->get['route'], $s) !== false) {
                        $match = true;
                        break;
                    }
                }
            }

            $dest_route = 'account/login';
            if (!$match) {
                if (!$this->customer->isLogged() && ($_SERVER['QUERY_STRING'] != "" && $_SERVER['QUERY_STRING'] != 'route=' . $dest_route)) {
                    $this->response->redirect($this->url->link($dest_route, '', 'SSL'));
                }
            }
            ]]></add>
        </operation>
    </file>

</modification>

for My above requirement. what all modification should I have to done. I am very new to opencart and vQmod. Thanking in advance.


Solution

  • //@route get the the current page
    
      $route = $this->request->get['route'];
    
    //check if customer is logged in else redirect to login
    
     if (!$this->customer->isLogged() && $route !='account/login' && $route !='account/forgotten' && $route !='account/register') {
       $this->response->redirect($this->url->link('account/login', '', true));
     }