Blawdi Posted September 23, 2015 Share Posted September 23, 2015 Hello, I need to display random product on module, and i have no idea. This is code on cshomefeature.php <?php if (!defined('_CAN_LOAD_FILES_')) exit; class CsHomeFeature extends Module { public function __construct() { $this->name = 'cshomefeature'; $this->tab = 'front_office_features'; $this->version = '1.0'; $this->author = 'CodeSpot'; $this->bootstrap = true; parent::__construct(); $this->displayName = $this->l('CS Products Category'); $this->description = $this->l('Displays Products by category in your homepage.'); } public function install() { $arrCheck="2,3"; if (!Configuration::updateValue('HOME_FEATURED_ID_CAT',$arrCheck,false,null,Configuration::get('PS_SHOP_DEFAULT')) OR !parent::install() OR !$this->registerHook('home') OR !$this->registerHook('header') OR !$this->registerHook('showthumnbnailimage') OR !Configuration::updateValue('HOME_FEATURED_NUMBER_PROD',6) OR !Configuration::updateValue('HOME_FEATURED_LAYOUT',0) OR !$this->registerHook('actionObjectProductUpdateAfter') OR !$this->registerHook('actionObjectProductDeleteAfter') OR !$this->registerHook('actionObjectProductAddAfter') OR !$this->registerHook('actionUpdateQuantity') OR !$this->registerHook('actionProductAdd') OR !$this->registerHook('actionProductDelete') OR !$this->registerHook('actionProductUpdate') ) return false; return true; } public function uninstall() { Configuration::deleteByName('HOME_FEATURED_ID_CAT'); Configuration::deleteByName('HOME_FEATURED_NUMBER_PROD'); if (!parent::uninstall()) return false; return true; } public function getContent() { $output = ''; if (Tools::isSubmit('submitMyHomeFeatured')) { $this->_clearCache('cshomefeature.tpl'); if(Tools::getValue('home_feature_number')!='') { Configuration::updateValue('HOME_FEATURED_NUMBER_PROD',Tools::getValue('home_feature_number')); } if(Tools::getValue('cshomefeature_layout')!='') { Configuration::updateValue('HOME_FEATURED_LAYOUT',Tools::getValue('cshomefeature_layout')); } if(Tools::getValue('categoryBox')!='') { $this->_clearCache('cshomefeature.tpl'); $cat = implode(",", Tools::getValue('categoryBox')); if (Shop::getContext() != Shop::CONTEXT_SHOP) foreach (Shop::getContextListShopID() as $id_shop) { Configuration::updateValue('HOME_FEATURED_ID_CAT', $cat,false,null,$id_shop); } else Configuration::updateValue('HOME_FEATURED_ID_CAT',$cat); } else Configuration::updateValue('HOME_FEATURED_ID_CAT', ''); if (isset($errors) AND sizeof($errors)) $output .= $this->displayError(implode('<br />', $errors)); else $output .= $this->displayConfirmation($this->l('Settings updated')); } return $output.$this->displayForm(); } public static function getCheckboxCatalog($arrCheck,$categories, $current, $id_category = 1, $has_suite = array()) { $done=array(); static $irow; if (!isset($done[$current['infos']['id_parent']])) $done[$current['infos']['id_parent']] = 0; $done[$current['infos']['id_parent']] += 1; if(isset($categories[$current['infos']['id_parent']])) $todo = sizeof($categories[$current['infos']['id_parent']]); $doneC = $done[$current['infos']['id_parent']]; $level = $current['infos']['level_depth'] + 1; if($id_category != 1) { $result = ' <tr class="'.($irow++ % 2 ? 'alt_row' : '').'"> <td> <input type="checkbox" name="categoryBox[]" class="categoryBox" id="categoryBox_'.$id_category.'" value="'.$id_category.'"'.(in_array($id_category, $arrCheck) ? ' checked="checked"' : '').'/> </td> <td> '.$id_category.' </td> <td>'; for ($i = 2; $i < $level; $i++) $result .= '<img src="../img/admin/lvl_'.$has_suite[$i - 2].'.gif" alt="" />'; $result .= '<img style="vertical-align:middle" src="../img/admin/'.($level == 1 ? 'lv1.gif' : 'lv2_'.($todo == $doneC ? 'f' : 'b').'.gif').'" alt="" /> <label for="categoryBox_'.$id_category.'" class="t">'.Tools::stripslashes($current['infos']['name']).'</label></td> </tr>'; } else $result = ''; if ($level > 1) $has_suite[] = ($todo == $doneC ? 0 : 1); if (isset($categories[$id_category])) foreach ($categories[$id_category] AS $key => $row) if ($key != 'infos') $result.= self::getCheckboxCatalog($arrCheck,$categories, $categories[$id_category][$key], $key, $has_suite); return $result; } public function displayForm() { $id_lang=$this->context->language->id; $categories = Category::getCategories((int)($id_lang), false); $id = (int)Context::getContext()->shop->id; $id_shop = $id ? $id: Configuration::get('PS_SHOP_DEFAULT'); if(Configuration::get('HOME_FEATURED_ID_CAT')=="") $str_id_cat="2,3"; else $str_id_cat=Configuration::get('HOME_FEATURED_ID_CAT'); $arrCheck = explode(",",$str_id_cat); $number_prod=Configuration::get('HOME_FEATURED_NUMBER_PROD'); $id_root = Configuration::get('PS_ROOT_CATEGORY',null,null,$id_shop); $id_home =$this->getRootCategoryForAShop(); $output = ' <form action="'.$_SERVER['REQUEST_URI'].'" method="post"> <div class="panel"> <div class="panel-heading">'.$this->displayName.'</div> <div class="form-group"> Mark all checkbox(es) of categories which have products appear in your homepage<sup> *</sup> </div> <div class="form-group"> <label class="control-label col-lg-3">'.$this->l('Layout style').'</label> <div class="col-lg-9"> <div class="col-sm-6"> <select name="cshomefeature_layout"> <option value="0" '.(Configuration::get('HOME_FEATURED_LAYOUT',null,null,$id_shop) == 0? 'selected="selected"' : '').' >Default</option> <option value="1" '.(Configuration::get('HOME_FEATURED_LAYOUT',null,null,$id_shop) ==1 ? 'selected="selected"' : '').' >Layout 1</option> <option value="2" '.(Configuration::get('HOME_FEATURED_LAYOUT',null,null,$id_shop) ==2 ? 'selected="selected"' : '').' >Layout 2</option> </select> <p class="help-block">'.$this->l('Support 3 layout for various home page. ').'</p> </div> </div> </div> <div class="form-group"> <label class="control-label col-lg-3">Number Product Display on a category:</label> <div class="col-lg-9"> <div class="col-sm-6"> <input type="text" name="home_feature_number" value="'.$number_prod.'" /> </div> </div> </div> <div class="form-group" > <div class="col-lg-12" style="margin-top:10px"> <div id="categoryList"> <table cellspacing="0" cellpadding="0" class="table"> <tr> <th class="col-lg-1">check</th> <th class="col-lg-1">ID</th> <th class="col-lg-10">Category</th> </tr>' .$this->getCheckboxCatalog($arrCheck,$categories,$categories[$id_root][$id_home],1). '</table> </div> </div> </div> <div class="form-group"> <center><button type="submit" name="submitMyHomeFeatured" value="'.$this->l('Save').'"> <i class="process-icon-save"></i> '.$this->l('Save').' </button> </center> </div> </div> </form> '; return $output; } function getRootCategoryForAShop() { return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue(' SELECT `id_category` FROM `'._DB_PREFIX_.'shop` WHERE `id_shop` = '.(int)$this->context->shop->id); } function preHook() { $category_list = array(); $result = array(); $link=new Link(); $num_prod=6; if(Configuration::get('HOME_FEATURED_NUMBER_PROD')>0) $num_prod=Configuration::get('HOME_FEATURED_NUMBER_PROD'); if(Configuration::get('HOME_FEATURED_ID_CAT') != '') $str_cat=Configuration::get('HOME_FEATURED_ID_CAT'); else $str_cat="2,3"; if($str_cat != '') { $id_cat = explode(",",$str_cat); foreach ($id_cat as $id) { $categories = new Category($id,(int)(Context::getcontext()->language->id)); $category_list[$id]['name_category'] = $categories->name; $category_list[$id]['id_category'] = $categories->id; $category_list[$id]['link_category']=$link->getCategoryLink($id); $categories->product_list = $categories->getProducts((int)(Context::getcontext()->language->id),1,$num_prod); $category_list[$id]['product_list'] = $categories->product_list; $prod_list=$category_list[$id]['product_list']; } } return $category_list; } function hookShowthumnbnailimage($params) { $prod=new Product($params['product']['id_product']); $imageAll=$prod->getImages((int)Context::getContext()->language->id); $this->context->smarty->assign(array( 'imageAll' => $imageAll, 'product'=>$params['product'] )); return $this->display(__FILE__, 'cshomefeature_thumbnail.tpl'); } function hookHome($params) { $category_list = array(); $layout=0; $layout=Configuration::get('HOME_FEATURED_LAYOUT'); $store='default'; $store=Configuration::get('CS_STORE'); $this->context->smarty->assign(array( 'csstore' => $store )); if($layout==1) { if (!$this->isCached('cshomefeature_layout1.tpl', $this->getCacheId('cshomefeature_layout1'))) { $category_list=$this->preHook(); $this->context->smarty->assign(array( 'category_list' => $category_list, 'sub_pre'=>'sub' )); } return $this->display(__FILE__, 'cshomefeature_layout1.tpl',$this->getCacheId('cshomefeature_layout1')); } else if ($layout==2) { if (!$this->isCached('cshomefeature_layout2.tpl', $this->getCacheId('cshomefeature_layout2'))) { $category_list=$this->preHook(); $this->context->smarty->assign(array( 'category_list' => $category_list, 'sub_pre'=>'sub' )); } return $this->display(__FILE__, 'cshomefeature_layout2.tpl',$this->getCacheId('cshomefeature_layout2')); } else { if (!$this->isCached('cshomefeature.tpl', $this->getCacheId('cshomefeature'))) { $category_list=$this->preHook(); $this->context->smarty->assign(array( 'category_list' => $category_list, 'sub_pre'=>'sub' )); } return $this->display(__FILE__, 'cshomefeature.tpl', $this->getCacheId('cshomefeature')); } } function hookHeader($params) { require_once(_PS_TOOL_DIR_.'mobile_Detect/Mobile_Detect.php'); $detect =new Mobile_Detect();; if($detect->isMobile()) $isMobileIp=1; else $isMobileIp=0; $this->context->smarty->assign(array( 'isMobileIp' =>$isMobileIp )); $id_hook=Hook::getIdByName('showthumnbnailimage'); $arrMod=Hook::getModulesFromHook($id_hook); if(!$arrMod || count($arrMod)<=0) { $this->registerHook('showthumnbnailimage'); } if ($this->context->smarty->tpl_vars['page_name']->value == 'index') $this->context->controller->addCSS($this->_path.'css/cshomefeature.css', 'screen'); } public function clearCacheHomeFeature($params) { $this->_clearCache('cshomefeature.tpl'); $this->_clearCache('cshomefeature_layout1.tpl'); $this->_clearCache('cshomefeature_layout2.tpl'); } public function hookActionObjectProductUpdateAfter($params) { $this->clearCacheHomeFeature($params); } public function hookActionObjectProductDeleteAfter($params) { $this->clearCacheHomeFeature($params); } public function hookActionUpdateQuantity($params) { $this->clearCacheHomeFeature($params); } public function hookactionProductAdd($params) { $this->clearCacheHomeFeature($params); } public function hookactionProductDelete($params) { $this->clearCacheHomeFeature($params); } public function hookactionProductUpdate($params) { $this->clearCacheHomeFeature($params); } } I did not dare to change the code with code similar to Homefeature there is some small difference.Can you help me? Link to comment Share on other sites More sharing options...
taydotech123 Posted September 23, 2015 Share Posted September 23, 2015 1. create a NEW class extends Category class in core of prestashop 2. override this method in New class: getProducts 3. user ORDER BY rand() in Mysql query like this: http://stackoverflow.com/questions/16289287/mysql-select-random-row-rand-performance you can get Products random in each Category Link to comment Share on other sites More sharing options...
Blawdi Posted September 23, 2015 Author Share Posted September 23, 2015 seriously? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now