Crear un módulo para PrestaShop es una tarea que requiere algunos conocimientos de PHP, HTML, CSS y, si es necesario, JavaScript. A continuación, te daré un ejemplo básico de cómo podrías estructurar un módulo para añadir un botón flotante de WhatsApp en una tienda PrestaShop. Por favor, asegúrate de ajustar el código según tus necesidades específicas y de seguir las prácticas recomendadas de PrestaShop para el desarrollo de módulos.
Estructura del Módulo
- Directorio del Módulo: Crea un nuevo directorio dentro de
modules, por ejemplo, whatsappbutton.
- Archivo principal del Módulo: Crea un archivo PHP, como
whatsappbutton.php.
Aquí tienes un ejemplo básico del contenido de 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 = 'Tu Nombre';
$this->need_instance = 0;
$this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_);
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('WhatsApp Floating Button');
$this->description = $this->l('Añade un botón flotante de WhatsApp a tu tienda.');
$this->confirmUninstall = $this->l('¿Estás seguro de que deseas desinstalar?');
if (!Configuration::get('WHATSAPP_BUTTON'))
$this->warning = $this->l('No se ha configurado el número de WhatsApp.');
}
public function install()
{
if (Shop::isFeatureActive()) {
Shop::setContext(Shop::CONTEXT_ALL);
}
if (!parent::install() ||
!$this->registerHook('displayFooter') ||
!Configuration::updateValue('WHATSAPP_BUTTON', '')
) {
return false;
}
return true;
}
public function uninstall()
{
if (!parent::uninstall() ||
!Configuration::deleteByName('WHATSAPP_BUTTON')
) {
return false;
}
return true;
}
public function hookDisplayFooter($params)
{
$whatsapp_number = Configuration::get('WHATSAPP_BUTTON');
$this->context->smarty->assign('whatsapp_number', $whatsapp_number);
return $this->display(__FILE__, 'views/templates/hook/whatsappbutton.tpl');
}
public function getContent()
{
$output = '';
if (Tools::isSubmit('submit'.$this->name)) {
$whatsapp_number = strval(Tools::getValue('WHATSAPP_BUTTON'));
if (!$whatsapp_number
|| empty($whatsapp_number)
|| !Validate::isGenericName($whatsapp_number)) {
$output .= $this->displayError($this->l('Número inválido de WhatsApp'));
} else {
Configuration::updateValue('WHATSAPP_BUTTON', $whatsapp_number);
$output .= $this->displayConfirmation($this->l('Configuración actualizada'));
}
}
return $output.$this->renderForm();
}
public function renderForm()
{
$fields_form = [
'form' => [
'legend' => [
'title' => $this->l('Settings'),
'icon' => 'icon-cogs'
],
'input' => [
[
'type' => 'text',
'label' => $this->l('WhatsApp Number'),
'name' => 'WHATSAPP_BUTTON',
'size' => 20,
'required' => true
]
],
'submit' => [
'title' => $this->l('Save'),
'class' => 'btn btn-default pull-right'
]
],
];
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
$lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
$helper->default_form_language = $lang->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
$helper->identifier = $this->identifier;
$helper->submit_action = 'submit'.$this->name;
$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 = [
'fields_value' => $this->getConfigFormValues(),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id
];
return $helper->generateForm([$fields_form]);
}
public function getConfigFormValues()
{
return [
'WHATSAPP_BUTTON' => Configuration::get('WHATSAPP_BUTTON'),
];
}
}
Crear la Plantilla para el Botón
Dentro del mismo directorio del módulo, crea una estructura de carpetas para las vistas: views/templates/hook/, y dentro crea el archivo whatsappbutton.tpl:
{if $whatsapp_number}
<div style="position: fixed; bottom: 20px; right: 20px;">
<a href="https://wa.me/{$whatsapp_number}" target="_blank">
<img src="https://example.com/path-to-whatsapp-icon.png" alt="WhatsApp" style="width: 50px; height: 50px;">
</a>
</div>
{/if}
Consideraciones Finales
- Asegúrate de ajustar la URL de la imagen del icono de WhatsApp a la dirección correcta o utiliza una librería de iconos como Font Awesome para referenciar el icono.
- El número de WhatsApp debe ser introducido en el formato internacional sin el signo
+.
- Este código es un ejemplo básico y se puede mejorar para manejar más configuraciones, validaciones, y estilos.
Recuerda que para instalar el módulo en tu tienda PrestaShop, debes comprimir la carpeta del módulo y subirla a través del panel de administración de PrestaShop. Después de subirlo, lo activas y configuras con el número de WhatsApp que deseas usar.