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; } $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 ]; if (!$this->isTemplateCached($cache_params)) { $currency = new Currency((int)($params['cookie']->id_currency)); // get all accessories of this product $other_products = array(); $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); $other_products[] = $accessory; } } if (empty($other_products)) { return ''; } $smarty->assign( array( '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 $other_products = array(); $bestsellers = ProductSale::getBestSalesInFamilyOf( $params['product']->id, $params['sale']->id, (int)($params['cookie']->id_lang), 0, 3, NULL, NULL, 10 ); 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( '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'; return $this->isCached( 'front_similarproducts.tpl', $cache_params['cache_id'], $cache_params['compile_id'] ); } private function flushTemplateCache($cache_params) { 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; } }