bebeboutik/modules/privatesales_similarproducts/privatesales_similarproducts.php

197 lines
4.4 KiB
PHP
Raw Normal View History

<?php
//if (!defined('_PS_VERSION_'))
// exit;
class Privatesales_similarproducts extends Module
{
private $_html = '';
private $_postErrors = array();
private $hooks = array(
'similarProduct' => array('Accessories or similar products', 'Called on displaying similar product in product details'),
);
public function __construct()
{
$this->name = 'privatesales_similarproducts';
$this->tab = 'front_office_features';
$this->version = '1.0';
$this->author = 'Antadis';
$this->need_instance = 0;
parent::__construct();
$this->displayName = $this->l('Produits complémentaires ou similaires');
$this->description = $this->l('Ajoute un bloc affichant les produits complémentaires (accessoires) ou les meilleures ventes de la famille d\'un produit.');
}
/**
* @see ModuleCore::install()
*/
public function install()
{
// Add custom hooks
foreach($this->hooks as $k => $v) {
if(count(Db::getInstance()->ExecuteS('
SELECT `id_hook`
FROM `'._DB_PREFIX_.'hook`
WHERE `name` = "'.$k.'"
LIMIT 1
')) == 0) {
Db::getInstance()->ExecuteS('
INSERT INTO `'._DB_PREFIX_.'hook`
VALUES (DEFAULT, "'.$k.'", "'.$v[0].'", "'.$v[1].'", 0, 0)
');
}
}
if (!parent::install() ||
!$this->registerHook('similarProduct')) {
$this->uninstall();
return false;
}
return true;
}
public function uninstall()
{
// Remove custom hooks
foreach($this->hooks as $k => $v) {
Db::getInstance()->Execute('
DELETE FROM `'._DB_PREFIX_.'hook`
WHERE `name` = "'.$k.'"
');
}
parent::uninstall();
}
public function hookSimilarProduct($params)
{
if(_PS_MOBILE_) {
return false;
}
2016-09-12 11:59:22 +02:00
$result = $this->displayAccessories($params);
if ($result=='') {
return $this->displayBestVPSalesSameFamily($params);
}
return $result;
}
private function displayAccessories($params)
{
global $smarty;
$cache_params = [
'prefix_cache' => '1_'.$params['product']->id
];
2016-09-12 11:59:22 +02:00
if (!$this->isTemplateCached($cache_params)) {
$currency = new Currency((int)($params['cookie']->id_currency));
// get all accessories of this product
2016-09-12 11:59:22 +02:00
$other_products = array();
2016-09-12 11:59:22 +02:00
$accessories = $params['product']->getAccessories($params['cookie']->id_lang);
if(is_array($accessories)) {
foreach ($accessories AS $accessory)
{
$accessory['price'] = Tools::displayPrice(Product::getPriceStatic((int)($accessory['id_product'])), $currency);
2016-09-12 11:59:22 +02:00
$other_products[] = $accessory;
}
}
2016-09-12 11:59:22 +02:00
if (empty($other_products)) {
return '';
}
$smarty->assign(
array(
2016-09-12 11:59:22 +02:00
'is_accessories' => TRUE,
'ps_other_products' => $other_products,
)
);
}
return $this->flushTemplateCache($cache_params);
}
private function displayBestVPSalesSameFamily($params)
{
global $smarty;
$cache_params = [
'prefix_cache' => '2_'.$params['sale']->id,
];
if (!$this->isTemplateCached($cache_params)) {
$currency = new Currency((int)($params['cookie']->id_currency));
// get all best sales of the product family
2016-09-12 11:59:22 +02:00
$other_products = array();
$bestsellers = ProductSale::getBestSalesInFamilyOf(
$params['sale']->id,
(int)($params['cookie']->id_lang), 0, 3, NULL, NULL, 10
);
2016-09-12 11:59:22 +02:00
if(is_array($bestsellers)) {
foreach ($bestsellers AS $bestseller) {
$bestseller['price'] = Tools::displayPrice(Product::getPriceStatic((int)($bestseller['id_product'])), $currency);
$other_products[] = $bestseller;
}
}
2016-09-12 11:59:22 +02:00
if (empty($other_products)) {
return '';
}
$smarty->assign(
array(
2016-09-12 11:59:22 +02:00
'is_accessories' => FALSE,
'ps_other_products' => $other_products,
)
);
}
return $this->flushTemplateCache($cache_params);
}
private function isTemplateCached(&$cache_params)
{
global $smarty;
global $cookie;
Tools::enableCache();
$smarty->cache_lifetime = 3600;
$cache_params['compile_id'] = $cache_params['prefix_cache'].'_'.(int)$cookie->id_lang;
$cache_params['cache_id'] = $cache_params['compile_id'].'_front_similarproducts';
2016-09-12 11:59:22 +02:00
return $this->isCached(
'front_similarproducts.tpl',
$cache_params['cache_id'],
$cache_params['compile_id']
);
}
private function flushTemplateCache($cache_params)
{
2016-09-12 11:59:22 +02:00
global $smarty;
$display = $this->display(
__FILE__,
'front_similarproducts.tpl' ,
$cache_params['cache_id'],
$cache_params['compile_id']
);
Tools::restoreCacheSettings();
$smarty->cache_lifetime = -1;
return $display;
}
}