<?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 Button');
$this->description = $this->l('Adds a customizable floating WhatsApp button.');
$this->ps_versions_compliancy = array('min' => '1.7.0.0', 'max' => _PS_VERSION_);
}
public function install()
{
return parent::install() &&
$this->registerHook('displayFooter') &&
$this->registerHook('actionAdminControllerSetMedia') &&
Configuration::updateValue('WHATSAPP_PHONE', '1234567890') &&
Configuration::updateValue('WHATSAPP_POSITION', 'bottom-right') &&
Configuration::updateValue('WHATSAPP_SHOW_DATES', json_encode(['start' => '', 'end' => '']));
}
public function uninstall()
{
return parent::uninstall() &&
Configuration::deleteByName('WHATSAPP_PHONE') &&
Configuration::deleteByName('WHATSAPP_POSITION') &&
Configuration::deleteByName('WHATSAPP_SHOW_DATES');
}
public function getContent()
{
$output = '';
if (Tools::isSubmit('submit'.$this->name)) {
$phone = strval(Tools::getValue('WHATSAPP_PHONE'));
$position = strval(Tools::getValue('WHATSAPP_POSITION'));
$dates = json_encode([
'start' => Tools::getValue('WHATSAPP_START_DATE'),
'end' => Tools::getValue('WHATSAPP_END_DATE')
]);
Configuration::updateValue('WHATSAPP_PHONE', $phone);
Configuration::updateValue('WHATSAPP_POSITION', $position);
Configuration::updateValue('WHATSAPP_SHOW_DATES', $dates);
$output .= $this->displayConfirmation($this->l('Settings updated'));
}
return $output.$this->renderForm();
}
public function renderForm()
{
// Form configuration
$fields_form = array(
'form' => array(
'legend' => array(
'title' => $this->l('Settings'),
),
'input' => array(
array(
'type' => 'text',
'label' => $this->l('WhatsApp Phone Number'),
'name' => 'WHATSAPP_PHONE',
'size' => 20,
'required' => true,
),
array(
'type' => 'select',
'label' => $this->l('Button Position'),
'name' => 'WHATSAPP_POSITION',
'options' => array(
'query' => array(
array('id' => 'bottom-right', 'name' => $this->l('Bottom Right')),
array('id' => 'bottom-left', 'name' => $this->l('Bottom Left')),
array('id' => 'top-right', 'name' => $this->l('Top Right')),
array('id' => 'top-left', 'name' => $this->l('Top Left')),
),
'id' => 'id',
'name' => 'name'
)
),
array(
'type' => 'date',
'label' => $this->l('Start Date'),
'name' => 'WHATSAPP_START_DATE',
),
array(
'type' => 'date',
'label' => $this->l('End Date'),
'name' => 'WHATSAPP_END_DATE',
)
),
'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->default_form_language = (int)Configuration::get('PS_LANG_DEFAULT');
$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 = array(
'fields_value' => $this->getConfigFieldsValues(),
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id
);
return $helper->generateForm(array($fields_form));
}
public function getConfigFieldsValues()
{
$dates = json_decode(Configuration::get('WHATSAPP_SHOW_DATES'), true);
return array(
'WHATSAPP_PHONE' => Tools::getValue('WHATSAPP_PHONE', Configuration::get('WHATSAPP_PHONE')),
'WHATSAPP_POSITION' => Tools::getValue('WHATSAPP_POSITION', Configuration::get('WHATSAPP_POSITION')),
'WHATSAPP_START_DATE' => Tools::getValue('WHATSAPP_START_DATE', $dates['start']),
'WHATSAPP_END_DATE' => Tools::getValue('WHATSAPP_END_DATE', $dates['end'])
);
}
public function hookDisplayFooter($params)
{
$dates = json_decode(Configuration::get('WHATSAPP_SHOW_DATES'), true);
$startDate = strtotime($dates['start']);
$endDate = strtotime($dates['end']);
$now = time();
if ($startDate && $endDate && ($now < $startDate || $now > $endDate)) {
return;
}
$this->context->smarty->assign(array(
'whatsapp_phone' => Configuration::get('WHATSAPP_PHONE'),
'whatsapp_position' => Configuration::get('WHATSAPP_POSITION'),
));
return $this->display(__FILE__, 'views/templates/hook/whatsappbutton.tpl');
}
}
Recuerda probar bien el módulo en un entorno de desarrollo antes de subirlo a producción. Esta es una base sencilla, y podrías expandirla aún más, agregando características como traducciones de texto, estilos personalizados, y más."""
USER: """Dame más completo que se pueda cambiar el número de teléfono desde el módulo y además que se pueda escoger la posición del botón y también que se pueda poner un calendario para mostrarlo cuando yo quiera"""
ASSISTANT: """Crear un módulo de PrestaShop que agregue un botón flotante de WhatsApp requiere conocimientos básicos de PHP, HTML, CSS y JavaScript. Aquí te dejo un ejemplo sencillo para comenzar: