Crear un módulo para PrestaShop que añada un botón flotante de WhatsApp implica varios pasos. Aquí tienes un ejemplo básico para un módulo llamado whatsappbutton:
1. Estructura del Módulo
Primero, debes tener la siguiente estructura de carpetas y archivos:
whatsappbutton/
│
├── whatsappbutton.php
├── logo.png
└── views/
└── templates/
└── hook/
└── displayFooter.tpl
2. Código del Módulo
a. Archivo principal (whatsappbutton.php)
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
class WhatsappButton extends Module
{
public function __construct()
{
$this->name = 'whatsappbutton';
$this->tab = 'front_office_features';
$this->version = '1.0.0';
$this->author = 'Your Name';
$this->need_instance = 0;
parent::__construct();
$this->displayName = $this->l('WhatsApp Floating Button');
$this->description = $this->l('Adds a floating WhatsApp button to the shop.');
}
public function install()
{
return parent::install() && $this->registerHook('displayFooter');
}
public function hookDisplayFooter($params)
{
$this->context->smarty->assign(array(
'whatsapp_number' => Configuration::get('WHATSAPP_NUMBER')
));
return $this->display(__FILE__, 'views/templates/hook/displayFooter.tpl');
}
}
b. Plantilla (displayFooter.tpl)
<div style="position:fixed;bottom:20px;right:20px;">
<a href="https://wa.me/{$whatsapp_number|escape:'html'}" target="_blank">
<img src="{$module_dir}views/img/whatsapp-icon.png" alt="WhatsApp" style="width:50px;height:50px;">
</a>
</div>
c. Imagen del icono de WhatsApp
Debes incluir una imagen de un icono de WhatsApp en la carpeta views/img/. Puedes descargar un icono de WhatsApp de internet o crear el tuyo propio y nombrarlo como whatsapp-icon.png.
3. Configuración del Módulo
Para configurar el número de WhatsApp que el botón debe usar, puedes ampliar el módulo y añadir un método para guardar configuraciones en la base de datos:
Añade esto a whatsappbutton.php para manejar la configuración:
public function getContent()
{
$output = null;
if (Tools::isSubmit('submit' . $this->name)) {
$whatsapp_number = strval(Tools::getValue('WHATSAPP_NUMBER'));
if (!$whatsapp_number || empty($whatsapp_number) || !Validate::isGenericName($whatsapp_number)) {
$output .= $this->displayError($this->l('Invalid Configuration value'));
} else {
Configuration::updateValue('WHATSAPP_NUMBER', $whatsapp_number);
$output .= $this->displayConfirmation($this->l('Settings updated'));
}
}
return $output . $this->displayForm();
}
public function displayForm()
{
$defaultLang = (int)Configuration::get('PS_LANG_DEFAULT');
$fieldsForm[0]['form'] = array(
'legend' => array(
'title' => $this->l('Settings'),
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('WhatsApp Number'),
'name' => 'WHATSAPP_NUMBER',
'size' => 20,
'required' => true
),
),
'submit' => array(
'title' => $this->l('Save'),
'class' => 'btn btn-default pull-right'
)
);
$helper = new HelperForm();
$helper->module = $this;
$helper->name_controller = $this->name;
$helper->identifier = $this->identifier;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex . '&configure=' . $this->name;
$helper->default_form_language = $defaultLang;
$helper->allow_employee_form_lang = $defaultLang;
$helper->title = $this->displayName;
$helper->show_toolbar = true;
$helper->toolbar_scroll = true;
$helper->submit_action = 'submit' . $this->name;
$helper->fields_value['WHATSAPP_NUMBER'] = Configuration::get('WHATSAPP_NUMBER');
return $helper->generateForm($fieldsForm);
}
4. Instalación del Módulo
- Comprime la carpeta
whatsappbutton en un archivo ZIP.
- Accede al panel de administración de PrestaShop.
- Ve a "Módulos y Servicios" > "Módulos y Servicios".
- Haz clic en "Añadir nuevo módulo" y sube el archivo ZIP.
- Busca el módulo
WhatsApp Floating Button en la lista y haz clic en "Instalar".
- Configura el número de teléfono desde la sección de configuración del módulo.
Esto te proporcionará un botón flotante de WhatsApp en la parte inferior derecha de tu tienda PrestaShop. Si quieres modificar el estilo o la posición, puedes ajustar el HTML y el CSS en el archivo displayFooter.tpl.