toutpratique/modules/homeproduct/homeproduct.php
Tout Pratique 5d50494bf6 recette
2015-11-17 10:12:52 +01:00

85 lines
2.4 KiB
PHP

<?php
if (!defined('_CAN_LOAD_FILES_'))
exit;
class Homeproduct extends Module
{
public $ids = array();
public function __construct()
{
$this->name = 'homeproduct';
$this->tab = 'front_office_features';
$this->version = '1.0';
$this->author = 'Antadis';
$this->need_instance = 0;
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Display product on Home');
$this->description = $this->l('Display product on Home');
$this->secure_key = Tools::encrypt($this->name);
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
}
public function install()
{
if (Shop::isFeatureActive())
Shop::setContext(Shop::CONTEXT_ALL);
if (!parent::install()
|| !$this->registerHook('displayHomeProduct')
|| !$this->registerHook('displayHomeProductBottom')
)
return false;
}
public function hookdisplayHomeProduct($params)
{
$products = $this->loadProduct(Context::getContext()->language->id, 2);
$this->smarty->assign(array(
'products' => $products,
));
return $this->display(__FILE__, 'home.tpl');
}
public function hookdisplayHomeProductBottom($params)
{
$products = $this->loadProduct(Context::getContext()->language->id, 2);
$this->smarty->assign(array(
'products' => $products,
));
return $this->display(__FILE__, 'home_bottom.tpl');
}
public function loadProduct($id_lang, $limit)
{
$collection = new Collection('Product', $id_lang);
$collection->where('active', '=', 1);
$collection->where('online_only', '=', 1);
if ($this->ids) {
foreach ($this->ids as $key => $id) {
$collection->where('id_product', '!=', (int) $id);
}
}
$collection->setPageSize($limit);
$collection->sqlOrderBy('RAND()');
$products = $collection->getResults();
foreach ($products as $key => $product) {
$this->ids[] = $product->id;
}
$products = array_map(function($elem) {
$elem = $elem->loadReductionInfo();
return get_object_vars($elem);
}, $products);
return $products;
}
}