Um eine zufällige Anordnung von Produkten im Featured Module zu erreichen, kann man die shuffle() Funktion wie folgt in homefeatured.tpl vor den for each-Loop einbauen
Ursprünglicher Code
<ul style="height:{$ulHeight}px;"> {foreach from=$products item=product name=homeFeaturedProducts}
Mit Shuffle Funktion
<ul style="height:{$ulHeight}px;"> {capture}{$products|@shuffle}{/capture} {foreach from=$products item=product name=homeFeaturedProducts}
Es wird also die Zeile {capture}{$products|@shuffle}{/capture} eingefügt.
Nun muss noch die Cache Funktion ausgeschaltet werden, damit nicht der Cache, sondern die zufällige Produktanordnung gezeigt wird. Dies geschieht in der Datei homefeatured.php
Hier die folgende Funktion suchen
public function hookDisplayHome($params) { if (!$this->isCached('homefeatured.tpl', $this->getCacheId())) { $category = new Category(Context::getContext()->shop->getCategory(), (int)Context::getContext()->language->id); $nb = (int)Configuration::get('HOME_FEATURED_NBR'); $products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), "position"); shuffle($products); $this->smarty->assign(array( 'products' => $products, 'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'), 'homeSize' => Image::getSize(ImageType::getFormatedName('home')), )); } return $this->display(__FILE__, 'homefeatured.tpl', $this->getCacheId());
Hier wird nun die Zeile if (!$this->isCached(‚homefeatured.tpl‘, $this->getCacheId())) mit zwei vorangestellten // auskommentiert. Ebenso die nachfolgende öffnende und schließende geschweifte Klammer. In der letzten Zeile wird das letzte Komma und der nachfolgende Parameter mit dem Cachehinweis entfernt , $this->getCacheId() so dass die Funktion dann wie folgt aussieht:
public function hookDisplayHome($params) { // if (!$this->isCached('homefeatured.tpl', $this->getCacheId())) // { $category = new Category(Context::getContext()->shop->getCategory(), (int)Context::getContext()->language->id); $nb = (int)Configuration::get('HOME_FEATURED_NBR'); $products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), "position"); shuffle($products); $this->smarty->assign(array( 'products' => $products, 'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'), 'homeSize' => Image::getSize(ImageType::getFormatedName('home')), )); // } return $this->display(__FILE__, 'homefeatured.tpl');
Die Shuffle Funktion kann auch in die Funktion displayHome eingebaut werden und muss dann nicht in die homefeatured.tpl eingefügt werden:
public function hookDisplayHome($params){ $category = new Category(Context::getContext()->shop->getCategory(), (int)Context::getContext()->language->id); $nb = (int)(Configuration::get('HOME_FEATURED_NBR')); /*$products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 10));*/ $products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 10), 'date_add', 'DESC', false, true, true, $nb); shuffle($products); $this->smarty->assign(array( 'products' => $products, 'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'), 'homeSize' => Image::getSize('home_default'), )); return $this->display(__FILE__, 'homefeatured.tpl'); }