coxmin Posted June 6, 2016 Share Posted June 6, 2016 Hello guys, I would like that the products show random order on each category page refresh.. Can anyone help me with this ? Thank you! Link to comment Share on other sites More sharing options...
shokinro Posted June 6, 2016 Share Posted June 6, 2016 You can try to change the line that get product list as following in file /controllers/front/CategpryController.php From: $this->cat_products = $this->category->getProducts($this->context->language->id, (int)$this->p, (int)$this->n, $this->orderBy, $this->orderWay); To: $this->cat_products = $this->category->getProducts($this->context->language->id, (int)$this->p, (int)$this->n, $this->orderBy, $this->orderWay, false, true, true); Link to comment Share on other sites More sharing options...
rocky Posted June 7, 2016 Share Posted June 7, 2016 That code change should work, but please note that you need to disable caching on the Advanced Parameters > Performance tab, otherwise you'll get the same set of random products each time you refresh the page. Link to comment Share on other sites More sharing options...
coxmin Posted June 7, 2016 Author Share Posted June 7, 2016 Hello guys, I did change the line as you told me but still not work. The cache was deactivated also. I am using custom theme, is that the problem ? Thank you, Cosmin Link to comment Share on other sites More sharing options...
coxmin Posted June 7, 2016 Author Share Posted June 7, 2016 This the last part of the code /** * Assigns product list template variables */ public function assignProductList() { $hook_executed = false; Hook::exec('actionProductListOverride', array( 'nbProducts' => &$this->nbProducts, 'catProducts' => &$this->cat_products, 'hookExecuted' => &$hook_executed, )); // The hook was not executed, standard working if (!$hook_executed) { $this->context->smarty->assign('categoryNameComplement', ''); $this->nbProducts = $this->category->getProducts(null, null, null, $this->orderBy, $this->orderWay, true); $this->pagination((int)$this->nbProducts); // Pagination must be call after "getProducts" $this->cat_products = $this->category->getProducts($this->context->language->id, (int)$this->p, (int)$this->n, $this->orderBy, $this->orderWay, false, true, true); } // Hook executed, use the override else { // Pagination must be call after "getProducts" $this->pagination($this->nbProducts); } $this->addColorsToProductList($this->cat_products); Hook::exec('actionProductListModifier', array( 'nb_products' => &$this->nbProducts, 'cat_products' => &$this->cat_products, )); foreach ($this->cat_products as &$product) { if (isset($product['id_product_attribute']) && $product['id_product_attribute'] && isset($product['product_attribute_minimal_quantity'])) { $product['minimal_quantity'] = $product['product_attribute_minimal_quantity']; } } $this->context->smarty->assign('nb_products', $this->nbProducts); } /** * Returns an instance of the current category * * @return Category */ public function getCategory() { return $this->category; } } Link to comment Share on other sites More sharing options...
rocky Posted June 7, 2016 Share Posted June 7, 2016 Reading the code, there must a module that is overwriting the product list on these lines: Hook::exec('actionProductListModifier', array( 'nb_products' => &$this->nbProducts, 'cat_products' => &$this->cat_products, )); You should check Modules > Positions to find out what modules are installed in the actionProductListModifier hook. One of those modules must be overwriting the product list. Link to comment Share on other sites More sharing options...
shokinro Posted June 7, 2016 Share Posted June 7, 2016 If you are using block layered navigation module, then the product list generation was hijacked by that module. You need to modify the following file /modules/blocklayered/blocklayered.php Link to comment Share on other sites More sharing options...
coxmin Posted June 7, 2016 Author Share Posted June 7, 2016 // Inform the hook was executed $params['hookExecuted'] = true; // List of product to overrride categoryController $params['catProducts'] = array(); $selected_filters = $this->getSelectedFilters(); $filter_block = $this->getFilterBlock($selected_filters); $title = ''; if (is_array($filter_block['title_values'])) foreach ($filter_block['title_values'] as $key => $val) $title .= ' > '.$key.' '.implode('/', $val); $smarty->assign('categoryNameComplement', $title); $this->getProducts($selected_filters, $params['catProducts'], $params['nbProducts'], $p, $n, $pages_nb, $start, $stop, $range); // Need a nofollow on the pagination links? $smarty->assign('no_follow', $filter_block['no_follow']); } I opened /modules/blocklayered/blocklayered.php . Should i modify something from above ? Link to comment Share on other sites More sharing options...
shokinro Posted June 7, 2016 Share Posted June 7, 2016 (edited) You need to make change to method getProductByFilters() of this module to make the query of product to order by a random number. Edited June 7, 2016 by shokinro (see edit history) Link to comment Share on other sites More sharing options...
coxmin Posted June 7, 2016 Author Share Posted June 7, 2016 And changing with this getProducts($this->context->language->id, (int)$this->p, (int)$this->n, $this->orderBy, $this->orderWay, false, true, true); it will work ? Link to comment Share on other sites More sharing options...
shokinro Posted June 7, 2016 Share Posted June 7, 2016 No, because it used different method, you have to modify the query itself, not the parameters of the method. Link to comment Share on other sites More sharing options...
soufhardnav Posted January 29, 2019 Share Posted January 29, 2019 Hello guys , the solution is that u must shuffel the products before return it at line 2092: ... GROUP BY product_shop.id_product ORDER BY '.Tools::getProductsOrder('by', Tools::getValue('orderby'), true).' '.Tools::getProductsOrder('way', Tools::getValue('orderway')).' , cp.id_product'. ' LIMIT '.(((int)$this->page - 1) * $n.','.$n), true, false); } } if (Tools::getProductsOrder('by', Tools::getValue('orderby'), true) == 'p.price') Tools::orderbyPrice($this->products, Tools::getProductsOrder('way', Tools::getValue('orderway'))); shuffle ($this->products); return $this->products; } Link to comment Share on other sites More sharing options...
R.ACE Posted August 20, 2019 Share Posted August 20, 2019 In category.php is the "RAND ()" option as a filter to order the products but the pagination doesn't work. It is possible to mix the products randomly and that the pagination also works? Link to comment Share on other sites More sharing options...
gokturk Posted May 1, 2021 Share Posted May 1, 2021 (edited) On 6/7/2016 at 1:42 AM, shokinro said: You can try to change the line that get product list as following in file /controllers/front/CategpryController.php From: $this->cat_products = $this->category->getProducts($this->context->language->id, (int)$this->p, (int)$this->n, $this->orderBy, $this->orderWay); To: $this->cat_products = $this->category->getProducts($this->context->language->id, (int)$this->p, (int)$this->n, $this->orderBy, $this->orderWay, false, true, true); Thanks dude, code is working But; It shows only one product. What should we do to show all products randomly Edit Solved: Edited May 1, 2021 by gokturk (see edit history) 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