1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| <?php
if (!defined('_PS_VERSION_')) {
exit;
}
class CustomButton extends Module
{
public function __construct()
{
$this->name = 'custombutton';
$this->tab = 'front_office_features';
$this->version = '1.0.0';
$this->author = 'VotreNom';
$this->need_instance = 0;
parent::__construct();
$this->displayName = $this->l('Bouton personnalisé');
$this->description = $this->l('Ajoute un bouton personnalisé sur la page produit.');
$this->controllers = array('admin');
// Appels des hooks
$this->registerHook('displayProductButtons');
$this->registerHook('actionAdminControllerSetMedia');
$this->registerHook('displayFooterProduct');
}
public function install()
{
if (!parent::install() ||
!$this->registerHook('displayFooterProduct') ||
!Db::getInstance()->execute('
CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'product_custombutton` (
`id_product` INT(10) UNSIGNED NOT NULL,
`custom_button_text` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id_product`)
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8;
')
) {
return false;
}
return true;
}
public function uninstall()
{
if (!parent::uninstall() ||
!Db::getInstance()->execute('DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'product_custombutton`;')
) {
return false;
}
return true;
}
// Hook pour afficher le champ personnalisé dans le back office
public function hookDisplayProductButtons($params)
{
$id_product = (int)$params['product']['id_product'];
$custom_button_text = Db::getInstance()->getValue('
SELECT `custom_button_text`
FROM `' . _DB_PREFIX_ . 'product_custombutton`
WHERE `id_product` = ' . $id_product
);
$this->context->smarty->assign(array(
'custom_button_text' => $custom_button_text,
'id_product' => $id_product,
));
return $this->display(__FILE__, 'views/templates/admin/custom_button_form.tpl');
}
// Hook pour ajouter les styles et scripts dans le back office
public function hookActionAdminControllerSetMedia()
{
$this->context->controller->addCSS($this->_path . 'views/css/admin/custom_button.css');
}
// Hook pour afficher le bouton personnalisé dans le front office
public function hookDisplayFooterProduct($params)
{
$id_product = (int)$params['product']['id_product'];
$custom_button_text = Db::getInstance()->getValue('
SELECT `custom_button_text`
FROM `' . _DB_PREFIX_ . 'product_custombutton`
WHERE `id_product` = ' . $id_product
);
if (!empty($custom_button_text)) {
$this->context->smarty->assign(array(
'custom_button_text' => $custom_button_text,
));
return $this->display(__FILE__, 'views/templates/front/custom_button.tpl');
}
// Afficher le bouton "Ajouter au panier" par défaut
return null;
}
} |
Partager