bebeboutik/modules/privatesales_similarproducts/privatesales_similarproducts.php
2016-11-16 15:50:39 +01:00

244 lines
5.8 KiB
PHP
Executable File

<?php
if (!defined('_PS_VERSION_'))
exit;
class Privatesales_similarproducts extends Module
{
const HAS_ACCESSORIES_UNKNOWN = 0;
const HAS_ACCESSORIES_YES = 1;
const HAS_ACCESSORIES_NO = 2;
private $has_accessories_state = self::HAS_ACCESSORIES_UNKNOWN;
private $family = null;
private $label = '';
private $hooks = array(
'similarProduct' => array('Accessories or similar products', 'Called on displaying similar product in product details'),
'simlarProductTabLabel' => array('Tab labl accessories or similar products', 'Called on displaying tab 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)
');
}
}
$install_success = parent::install();
if ($install_success) {
foreach($this->hooks as $k => $v) {
$install_success = $this->registerHook($k);
if (!$install_success) {
break;
}
}
}
if (!$install_success) {
$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;
}
$this->initFromHook($params);
if ($this->has_accessories_state == self::HAS_ACCESSORIES_YES) {
return $this->displayAccessories($params);
}
if ($this->has_accessories_state == self::HAS_ACCESSORIES_NO) {
return $this->displayBestVPSalesSameFamily($params);
}
return '';
}
public function hookSimlarProductTabLabel($params)
{
if(_PS_MOBILE_) {
return false;
}
$this->initFromHook($params);
return $this->label;
}
private function initFromHook(array &$params)
{
if ($this->has_accessories_state != self::HAS_ACCESSORIES_UNKNOWN) {
return;
}
$query = '
SELECT COUNT(`id_product_2`)
FROM `'._DB_PREFIX_.'accessory`
JOIN `'._DB_PREFIX_.'product` p ON p.`id_product` = `id_product_2`
WHERE `id_product_1` = '.(int)$params['product']->id.'
AND p.`active` = 1
';
if (Db::getInstance()->getValue($query)>0) {
$this->has_accessories_state = self::HAS_ACCESSORIES_YES;
$this->label = $this->l('Produits complémentaires');
}
else {
$this->has_accessories_state = self::HAS_ACCESSORIES_NO;
$this->family = ProductSale::getCategoryFamily($params['category'], $params['sale']->id, $params['cookie']->id_lang);
if (!empty($this->family)) {
$bestsellers = ProductSale::getFamilyBestSales($this->family['id_category_family'], $params['cookie']->id_lang,3, 10,$params['product']->id);
//$this->label = $this->l('Autres produits dans').' '.$this->family['name'];
if(is_array($bestsellers) && !empty($bestsellers)) {
$this->label = $this->l('À découvrir');
}
}
}
}
private function displayAccessories($params)
{
global $smarty;
$cache_id = (int)$params['cookie']->id_lang.'_'.(int)$params['product']->id;
if (!$this->isTemplateCached($cache_id)) {
$other_products = array();
$accessories = $params['product']->getAccessories($params['cookie']->id_lang);
if(is_array($accessories)) {
$currency = new Currency((int)($params['cookie']->id_currency));
foreach ($accessories AS $accessory)
{
$accessory['price'] = Tools::displayPrice(Product::getPriceStatic((int)($accessory['id_product'])), $currency);
$other_products[] = $accessory;
}
}
if (empty($other_products)) {
return '';
}
$smarty->assign(
array(
'label' => $this->label,
'ps_other_products' => $other_products,
)
);
}
return $this->flushTemplateCache($cache_id);
}
private function displayBestVPSalesSameFamily($params)
{
global $smarty;
$cache_id = (int)$params['cookie']->id_lang.'_'.(int)$params['product']->id;
if (!$this->isTemplateCached($cache_id)) {
$currency = new Currency((int)($params['cookie']->id_currency));
// get all best sales of the product family
$other_products = array();
$bestsellers = ProductSale::getFamilyBestSales(
$this->family['id_category_family'],
$params['cookie']->id_lang,
3, 10,
$params['product']->id
);
if(is_array($bestsellers)) {
foreach ($bestsellers AS $bestseller) {
$bestseller['price'] = Tools::displayPrice(Product::getPriceStatic((int)($bestseller['id_product'])), $currency);
$other_products[] = $bestseller;
}
}
if (empty($other_products)) {
return '';
}
$smarty->assign(
array(
'label' => $this->label,
'ps_other_products' => $other_products,
)
);
}
return $this->flushTemplateCache($cache_id);
}
private function isTemplateCached(&$cache_id)
{
global $smarty;
Tools::enableCache();
$smarty->cache_lifetime = 3600;
return $this->isCached('front_similarproducts.tpl', $cache_id);
}
private function flushTemplateCache($cache_id)
{
global $smarty;
$display = $this->display(__FILE__, 'front_similarproducts.tpl', $cache_id);
Tools::restoreCacheSettings();
$smarty->cache_lifetime = -1;
return $display;
}
}