* @copyright 2007-2011 PrestaShop SA * @version Release: $Revision: 6594 $ * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PrestaShop SA */ if (!defined('_CAN_LOAD_FILES_')) exit; class productsCategory extends Module { private $_html; public function __construct() { $this->name = 'productscategory'; $this->version = '1.3'; $this->author = 'PrestaShop'; $this->tab = 'front_office_features'; $this->need_instance = 0; parent::__construct(); $this->displayName = $this->l('Products Category'); $this->description = $this->l('Display products of the same category on the product page.'); if (!$this->isRegisteredInHook('header')) $this->registerHook('header'); } public function install() { if (!parent::install() OR !$this->registerHook('productfooter') OR !$this->registerHook('header') OR !Configuration::updateValue('PRODUCTSCATEGORY_DISPLAY_PRICE', 0)) return false; return true; } public function uninstall() { if (!parent::uninstall() OR !Configuration::deleteByName('PRODUCTSCATEGORY_DISPLAY_PRICE')) return false; return true; } public function getContent() { $this->_html = ''; if (Tools::isSubmit('submitCross') AND Tools::getValue('displayPrice') != 0 AND Tools::getValue('displayPrice') != 1) $this->_html .= $this->displayError('Invalid displayPrice'); elseif (Tools::isSubmit('submitCross')) { Configuration::updateValue('PRODUCTSCATEGORY_DISPLAY_PRICE', Tools::getValue('displayPrice')); $this->_html .= $this->displayConfirmation($this->l('Settings updated successfully')); } $this->_html .= '
'.$this->l('Settings').'

'.$this->l('Show the price on the products in the block.').'

'; return $this->_html; } private function getCurrentProduct($products, $id_current) { if ($products) foreach ($products AS $key => $product) if ($product['id_product'] == $id_current) return $key; return false; } public function hookProductFooter($params) { global $smarty, $cookie; $idProduct = (int)(Tools::getValue('id_product')); $product = new Product((int)($idProduct)); /* If the visitor has came to this product by a category, use this one */ if (isset($params['category']->id_category)) $category = $params['category']; /* Else, use the default product category */ else { if (isset($product->id_category_default) AND $product->id_category_default > 1) $category = New Category((int)($product->id_category_default)); } if (!Validate::isLoadedObject($category) OR !$category->active) return; // Get infos $categoryProducts = $category->getProducts((int)($cookie->id_lang), 1, 100); /* 100 products max. */ $sizeOfCategoryProducts = (int)sizeof($categoryProducts); $middlePosition = 0; // Remove current product from the list if (is_array($categoryProducts) AND sizeof($categoryProducts)) { foreach ($categoryProducts AS $key => $categoryProduct) if ($categoryProduct['id_product'] == $idProduct) { unset($categoryProducts[$key]); break; } $taxes = Product::getTaxCalculationMethod(); if (Configuration::get('PRODUCTSCATEGORY_DISPLAY_PRICE')) foreach ($categoryProducts AS $key => $categoryProduct) if ($categoryProduct['id_product'] != $idProduct) { if ($taxes == 0 OR $taxes == 2) $categoryProducts[$key]['displayed_price'] = Product::getPriceStatic((int)$categoryProduct['id_product'], true, NULL); elseif ($taxes == 1) $categoryProducts[$key]['displayed_price'] = Product::getPriceStatic((int)$categoryProduct['id_product'], false, NULL); } // Get positions $middlePosition = round($sizeOfCategoryProducts / 2, 0); $productPosition = $this->getCurrentProduct($categoryProducts, (int)$idProduct); // Flip middle product with current product if ($productPosition) { $tmp = $categoryProducts[$middlePosition-1]; $categoryProducts[$middlePosition-1] = $categoryProducts[$productPosition]; $categoryProducts[$productPosition] = $tmp; } // If products tab higher than 30, slice it if ($sizeOfCategoryProducts > 30) { $categoryProducts = array_slice($categoryProducts, $middlePosition - 15, 30, true); $middlePosition = 15; } } // Display tpl $smarty->assign(array( 'categoryProducts' => $categoryProducts, 'middlePosition' => (int)$middlePosition, 'ProdDisplayPrice' => Configuration::get('PRODUCTSCATEGORY_DISPLAY_PRICE'))); return $this->display(__FILE__, 'productscategory.tpl'); } public function hookHeader($params) { Tools::addCSS($this->_path.'productscategory.css', 'all'); Tools::addJS(array($this->_path.'productscategory.js', _PS_JS_DIR_.'jquery/jquery.serialScroll-1.2.2-min.js')); } }