Crear un módulo en PrestaShop para añadir un botón flotante de WhatsApp requiere algunos pasos. A continuación, te proporciono un ejemplo básico de cómo podrías crear este módulo. Ten en cuenta que este es solo un punto de partida y deberías personalizarlo según tus necesidades específicas.
Paso 1: Crear la estructura del módulo
-
Crea una carpeta para tu módulo en la carpeta modules de PrestaShop. Nombra esta carpeta, por ejemplo, whatsappbutton.
-
Dentro de esta carpeta, crea un archivo PHP con el mismo nombre que la carpeta, whatsappbutton.php.
-
Crea un archivo de configuración como config.xml si deseas proporcionar información adicional sobre el módulo (opcional pero recomendado).
Estructura del archivo 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.0.0', 'max' => _PS_VERSION_);
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('WhatsApp Button');
$this->description = $this->l('Adds a floating WhatsApp button.');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
if (!Configuration::get('WHATSAPP_BUTTON')) {
$this->warning = $this->l('No WhatsApp contact number provided');
}
}
public function install()
{
return parent::install() &&
$this->registerHook('displayHeader') &&
Configuration::updateValue('WHATSAPP_BUTTON_PHONE', ''); // Default phone number
}
public function uninstall()
{
return parent::uninstall() && Configuration::deleteByName('WHATSAPP_BUTTON_PHONE');
}
public function getContent()
{
$output = '';
if (Tools::isSubmit('submitWhatsappButton')) {
$phone = Tools::getValue('WHATSAPP_BUTTON_PHONE');
if (empty($phone) || !Validate::isPhoneNumber($phone)) {
$output .= $this->displayError($this->l('Invalid phone number'));
} else {
Configuration::updateValue('WHATSAPP_BUTTON_PHONE', $phone);
$output .= $this->displayConfirmation($this->l('Settings updated'));
}
}
return $output . $this->renderForm();
}
protected function renderForm()
{
$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->l('Settings'),
'icon' => 'icon-cogs',
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('WhatsApp Phone Number'),
'name' => 'WHATSAPP_BUTTON_PHONE',
'size' => 20,
'required' => true,
),
),
'submit' => array(
'title' => $this->l('Save'),
'class' => 'btn btn-default pull-right',
),
),
);
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
$helper->module = $this;
$helper->identifier = $this->identifier;
$helper->submit_action = 'submitWhatsappButton';
$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->languages = $this->context->controller->getLanguages();
$helper->default_form_language = $this->context->language->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
$helper->fields_value['WHATSAPP_BUTTON_PHONE'] = Configuration::get('WHATSAPP_BUTTON_PHONE');
return $helper->generateForm(array($fields_form));
}
public function hookDisplayHeader($params)
{
$this->context->controller->addCSS($this->_path.'views/css/whatsappbutton.css', 'all');
}
public function hookDisplayFooter($params)
{
if (!Configuration::get('WHATSAPP_BUTTON_PHONE')) {
return;
}
$this->context->smarty->assign(
array(
'whatsapp_phone' => Configuration::get('WHATSAPP_BUTTON_PHONE'),
)
);
return $this->display(__FILE__, 'views/templates/hook/whatsappbutton.tpl');
}
}
Paso 2: Crear los archivos adicionales
- CSS: Crea un archivo CSS en
views/css/whatsappbutton.css que defina el estilo del botón flotante.
.whatsapp-button {
position: fixed;
bottom: 20px;
right: 20px;
z-index: 1000;
}
.whatsapp-button a {
background-color: #25d366;
color: #fff;
padding: 10px 15px;
border-radius: 50%;
text-align: center;
box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3);
font-size: 20px;
}
- Plantilla: Crea un archivo de plantilla en
views/templates/hook/whatsappbutton.tpl.
<div class="whatsapp-button">
<a href="https://wa.me/{$whatsapp_phone|escape:'html':'UTF-8'}" target="_blank">
<i class="fa fa-whatsapp"></i>
</a>
</div>
Paso 3: Instalar y configurar el módulo
-
Sube tu módulo a la carpeta modules de tu instalación de PrestaShop.
-
Ve al panel de administración de PrestaShop y navega a la sección de Módulos.
-
Busca tu módulo 'WhatsApp Button' e instálalo.
-
Configura tu número de WhatsApp desde la configuración del módulo.
Este ejemplo te da una idea básica. Puedes expandirlo para agregar más opciones de configuración y adaptarlo visualmente según tus requerimientos. Asegúrate de probar bien cualquier modificación antes de implementarla en un entorno de producción.