lucasdemenezes Posted December 29, 2014 Share Posted December 29, 2014 hi, do anyone know how to get random category products? i want to know when i open a category, those products change them positions every time i open. do anyone knows? i use prestashop 1.5.4 and 1.6 Link to comment Share on other sites More sharing options...
NemoPS Posted December 29, 2014 Share Posted December 29, 2014 You need to change the code in your categorycontroller.php file, 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); 1 Link to comment Share on other sites More sharing options...
Totti Posted December 29, 2014 Share Posted December 29, 2014 These parameters thing signify?I can not find a guide about. , false, true, true); Link to comment Share on other sites More sharing options...
NemoPS Posted December 29, 2014 Share Posted December 29, 2014 Just inspect the getProducts method, the best way to find it out public function getProducts($id_lang, $p, $n, $order_by = null, $order_way = null, $get_total = false, $active = true, $random = false, $random_number_products = 1, $check_access = true, Context $context = null) Link to comment Share on other sites More sharing options...
lucasdemenezes Posted December 29, 2014 Author Share Posted December 29, 2014 (edited) hi friend, thank you to your reply. about it: You need to change the code in your categorycontroller.php file, 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); i done this, but now, my categories are showwing just one product per page. the products are random, but, just one per page. i did it in prestashop 1.5.4 Edited December 29, 2014 by lucasdemenezes (see edit history) 1 Link to comment Share on other sites More sharing options...
NemoPS Posted December 30, 2014 Share Posted December 30, 2014 add another parameter for the number of random products you want then like' $this->cat_products = $this->category->getProducts($this->context->language->id, (int)$this->p, (int)$this->n, $this->orderBy, $this->orderWay, false, true, true, 10); 2 Link to comment Share on other sites More sharing options...
lucasdemenezes Posted December 30, 2014 Author Share Posted December 30, 2014 Nemo1 is a great guy. He solve my problem. thank you so much, my friend Link to comment Share on other sites More sharing options...
lucasdemenezes Posted January 1, 2015 Author Share Posted January 1, 2015 Hi nemo1, thanks for help. I put this code you sent for me in 7 prestashop websites, but just in one that code work. do know why? Link to comment Share on other sites More sharing options...
vekia Posted January 2, 2015 Share Posted January 2, 2015 with solution like this one your customers can (while browsing category pages 1,2,3,4, etc.) - see products twice (or more) - dont see some products let me explain, for example: we have category "t-shirts" This category has got 10 products, let's say p1,p2,p3,p4,p5,p6,p7,p8,p9,p10 we display 5 products per page, so "t-shirts" category will have 2 pages if our catalog displays products randomly, first page will show (example): p1,p5,p6,p9,p10 second page will show: p1,p2,p3,p9,p10 as you can see p4,p7,p8 will not appear and p1, p9, p10 appeared twice (on page 1 and page 2) Link to comment Share on other sites More sharing options...
NemoPS Posted January 3, 2015 Share Posted January 3, 2015 Yes, but there is no other solution if they want products random, without doing some huge modifications to prevent the same ones being displayed again also, since it really is random all products will be shown sooner or later, it's a matter of chance Link to comment Share on other sites More sharing options...
zubair1369 Posted January 3, 2015 Share Posted January 3, 2015 Hello Nemo1,Below code is not working in PrestaShop 1.6.Please advise.$this->cat_products = $this->category->getProducts($this->context->language->id, (int)$this->p, (int)$this->n, $this->orderBy, $this->orderWay, false, true, true, 10); Link to comment Share on other sites More sharing options...
NemoPS Posted January 5, 2015 Share Posted January 5, 2015 As I said, it might not work if you use caching. Can you try disabling it from the back office just as a test? Advanced Parameters -> performance Link to comment Share on other sites More sharing options...
Goyo Posted January 8, 2015 Share Posted January 8, 2015 Hi Nemo1 and vekia, this is a very interesting alternative for the shops, do you have any idea how to solve the problem about the duplicate and not appear products? Thanks in advance. Goyo.Note: I test this code with 1.5.6.2, 1.6.0.9 and It's running fine but with 1.6.0.11 It isn't working. Link to comment Share on other sites More sharing options...
loisnegrette Posted January 8, 2015 Share Posted January 8, 2015 Excuse my bad english, (i used google translator) You could replace the value 10, by Product variable determines when the user selects the amount of product to deploy? Link to comment Share on other sites More sharing options...
NemoPS Posted January 9, 2015 Share Posted January 9, 2015 @Goyo As I said it's all a matter or randomness. They will appear sooner or later, and the rate is equal for all as it's totally random, but of course it depends on how many products you want to display. The only way would be to cache the found ones into a cookie and exclude them for the search on other pages. But it's complicated really, and not very seo friendly at all Link to comment Share on other sites More sharing options...
Goyo Posted January 24, 2015 Share Posted January 24, 2015 Thanks Nemo1 for your answer. I'm sorry don't answer before, I had health problems. Regards. Link to comment Share on other sites More sharing options...
dirsubmit Posted July 13, 2016 Share Posted July 13, 2016 HI Guys, No matter what I do and change in /classes/Category.php nothing changes on my front-end. Cache disabled, cleared. $random set true .. even modified the SQL statements and add order by clauses.. no matter what.. the products in the categories come up the same exact way.. is something overriding what I'm doing? Does anybody have any clues as to why and where I should look? Tried all changes from everybody's post here and still no change on the way the products come up. Always in the same order. Using PS V 1.5.6.2 Thanks! Link to comment Share on other sites More sharing options...
vekia Posted July 14, 2016 Share Posted July 14, 2016 have you checked overrides/classes/Category.php ? maybe you've got an override of category class Link to comment Share on other sites More sharing options...
dirsubmit Posted July 15, 2016 Share Posted July 15, 2016 Hi Vekia, Yes I did Only files there are Cart / Dispacher / Index / Link / Product no category Any other ideas? I'm at a loss.. don't understand why when even when I modify SQL queries or any order by's still no change in the way the products are showing up in the categories. Thanks Link to comment Share on other sites More sharing options...
vekia Posted July 15, 2016 Share Posted July 15, 2016 and what is going on if you, for example - for test purposes only - change category.php to _category.php do you see error then? Link to comment Share on other sites More sharing options...
dirsubmit Posted July 16, 2016 Share Posted July 16, 2016 I haven't tried that.. will try it and let you know.. but if I create an error in category.php the site stops working and I get an error.. so for example if I put a X in the beginning of the file.. I get php error. Thanks. Link to comment Share on other sites More sharing options...
dirsubmit Posted July 16, 2016 Share Posted July 16, 2016 Just tried it and I get an error.. Fatal error: Class 'Category' not found in /var/www/ .. so any ideas now .. I completely don't get why it would be behaving this way? Thanks! Link to comment Share on other sites More sharing options...
NemoPS Posted July 26, 2016 Share Posted July 26, 2016 dirsubmit, that happened when you renamed the class category.php to _category.php. You need to clear the class_index.php file inside cache/ if you renamed it back Link to comment Share on other sites More sharing options...
dirsubmit Posted July 29, 2016 Share Posted July 29, 2016 Hi NemoPS, I fixed that problem and put the site back up.. by "any ideas now" I meant.. obviously it is that file being served / used because when I changed the name the site threw an error. So if that is the file being used then how come when I make the modifications you suggested, or any other modification.. clear all files / caches and the products in the categories still come up exactly the same way? Thanks Link to comment Share on other sites More sharing options...
NemoPS Posted July 30, 2016 Share Posted July 30, 2016 Can you try using die('test'); in the method you modify, and see if it dies? You might also want to check the same method is not overridden already Link to comment Share on other sites More sharing options...
dirsubmit Posted August 7, 2016 Share Posted August 7, 2016 Nemo, Ok I tried it. when I put die('test') inside where you've suggested the code change.. nothing happens.. so that whole condition isn't getting triggered.. when I put die('test') somewhere else.. in another block or condition.. it works.. dies.. to make things easier here's the code I have.. maybe it's different or something there you might be able to spot by seeing the whole thing! /** * Assign list of products template vars */ public function assignProductList() { $hookExecuted = false; Hook::exec('actionProductListOverride', array( 'nbProducts' => &$this->nbProducts, 'catProducts' => &$this->cat_products, 'hookExecuted' => &$hookExecuted, )); // The hook was not executed, standard working if (!$hookExecuted) { $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); die('test'); } // Hook executed, use the override else // Pagination must be call after "getProducts" $this->pagination($this->nbProducts); foreach ($this->cat_products as &$product) { if ($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); } /** * Get instance of current category */ public function getCategory() { return $this->category; } } Thanks! Link to comment Share on other sites More sharing options...
NemoPS Posted August 8, 2016 Share Posted August 8, 2016 AH, are you using layered navigation? DIsable it if so. Link to comment Share on other sites More sharing options...
dirsubmit Posted August 9, 2016 Share Posted August 9, 2016 Hi Nemo, Yes I am.. but can't really disable it.. the whole site structure is currently based on it... is the Layered navigation overriding the sorting? Thank you! Link to comment Share on other sites More sharing options...
NemoPS Posted August 9, 2016 Share Posted August 9, 2016 You can modify it though, there is a hook for product list override that you might want to edit Link to comment Share on other sites More sharing options...
iacobcat Posted April 25, 2017 Share Posted April 25, 2017 i put: <div style="display:none">{shuffle($products)}</div> in product-list.tpl and all it`s working ! Link to comment Share on other sites More sharing options...
dirsubmit Posted April 26, 2017 Share Posted April 26, 2017 Hi, What part of product-list.tpl did you put that? I've tried several times to do this and it has never worked.. I've tried every suggestion listed in this tread.. and more.. nothing changes no matter what I do! Also what version of PS are you using? If you could please show the code where you added that specifically that would greatly help others. Thanks Link to comment Share on other sites More sharing options...
iacobcat Posted April 28, 2017 Share Posted April 28, 2017 Prestashop 1.6.1.6 So: if you put in product-list.tpl, the sort menu won`t work so i found solution: in: product-sort.tpl put: (like so) tell me if it's working <option value="{if $page_name != 'best-sales'}{$orderbydefault|escape:'html':'UTF-8'}:{$orderwaydefault|escape:'html':'UTF-8'}{/if}"{if !in_array($orderby, array('price', 'name', 'quantity', 'reference')) && $orderby eq $orderbydefault} selected="selected" <div style="display:none">{shuffle($products)}</div> {/if}--</option> Link to comment Share on other sites More sharing options...
Recommended Posts