Prestashop 1.7 – Mostrar la tienda sólo a usuario logueados

Prestashop 1.7 – Mostrar la tienda sólo a usuario logueados

Prestashop

Módulo para Prestashop 1.7 que permite establecer una tienda online accesible sólo a clientes registrados.

El siguiente código es un módulo para Prestashop 1.7 que comprueba si un visitante está logueado o no. En caso negativo, le redirige a la página de login.

Fontend

Es un módulo bastante sencillo, consistente en comprobar en el header de cada página el nombre del controlador que se está cargando. Si el controlador es el de autenticación o el de restablecer contraseña, permitiremos el acceso.

Backend

En el apartado del backend o backoffice, crearemos un pequeño formulario por medio del cual se establecerá si esta funcionalidad se activa o no.

Este módulo es un pequeño ejemplo de cómo conseguir la funcionalidad, pero es ampliamente personalizable. Por ejemplo, podríamos poner en el backend un listado de controladores y que el usuario elija cuales se permiten y cuales no sin estar logueado.

Link de descarga: https://code.adaweb.es/downloads/onlyregisteredcustomer.zip

<?php
/**
* 2007-2019 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author    PrestaShop SA <contact@prestashop.com>
*  @copyright 2007-2019 PrestaShop SA
*  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*/


if (!defined('_PS_VERSION_')) { 
  exit;
}


class onlyregisteredcustomer extends Module
{

  public function __construct(){

    $this->name = 'onlyregisteredcustomer';
    $this->tab = 'content_management';
    $this->version = '1.0.0';
    $this->author = 'Adaweb';
    $this->need_instance = 1;
    $this->bootstrap = true;
    $this->_path = '/modules/onlyregisteredcustomer/';
    parent::__construct();

    $this->displayName = $this->l('Only registered customers');
    $this->description = $this->l('Set your shop visible only for logged customers');
    $this->confirmUninstall = $this->l('¿Are you sure? All data can be deleted');
    $this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_);

  }




  public function install(){    
    Configuration::updateValue("orc_status", false);

    return parent::install() &&
    $this->registerHook('header');   

  }




  public function uninstall(){
    Configuration::deleteByName("orc_status");
    return parent::uninstall();

  }




  public function getContent(){

    if ( ( (bool)Tools::isSubmit('orc_submit') ) == true ){

     $form_values = $this->getConfigFormValues();
     if(isset($form_values["orc_status"])){
      Configuration::updateValue("orc_status", Tools::getValue("orc_status"));
    }

  }


  return $this->renderForm();

}



protected function renderForm(){

  $helper = new HelperForm();

  $helper->show_toolbar = true;
  $helper->table = $this->table;
  $helper->module = $this;
  $helper->default_form_language = $this->context->language->id;
  $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0);

  $helper->identifier = $this->identifier;
  $helper->submit_action = 'orc_submit';
  $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false)
  .'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
  $helper->token = Tools::getAdminTokenLite('AdminModules');

  $helper->tpl_vars = array(
   'fields_value' => $this->getConfigFormValues(),
   'languages' => $this->context->controller->getLanguages(),
   'id_language' => $this->context->language->id,
 );

  return $helper->generateForm(array($this->getConfigForm()));

}


protected function getConfigForm(){
  return array(
    'form' => array(
      'legend' => array(
        'title' => $this->l('Configure'),
        'icon' => 'icon-cogs',
      ),           
      'input' => array(
        array(
          'type' => 'radio',
          'name' => 'orc_status',
          'label' => $this->l('Stablish visibility of your shop only for logged users?'),
          'lang' => false,
          'required' => true,
          'class' => '',
          'values' => array(
            array(
              'id' => 0,
              'value' => 0,
              'label' => $this->l('No'),
            ),
            array(
              'id' => 1,
              'value' => 1,
              'label' => $this->l('Yes'),
            ),
          ),
        ),
      ),                
      'submit' => array(
        'title' => $this->l('Save'),
      ),
    ),
  );
}



protected function getConfigFormValues(){

  return array(       
    'orc_status' => Configuration::get('orc_status', true)
  );

}








/* HOOK */

public function hookHeader(){

  $allowed_controller = array("authentication", "password");

  if( Configuration::get('orc_status')){
    if( ($this->context->customer->isLogged() === false) AND (!in_array(Tools::getValue('controller'), $allowed_controller )) ){
      header('Location: '.$this->context->link->getPageLink('authentication', true));
      exit();

    }
  }


}







}

Link de descarga: https://code.adaweb.es/downloads/onlyregisteredcustomer.zip

Leave a reply