Jump to content

random products on category page


Recommended Posts

  • 1 month later...

The easiest way is to change line 59 of category.php (in Prestashop v1.2.5) from:

$cat_products = $category->getProducts(intval($cookie->id_lang), intval($p), intval($n), $orderBy, $orderWay);



to:

if ($orderBy == 'position')
   $cat_products = $category->getProducts(intval($cookie->id_lang), intval($p), intval($n), $orderBy, $orderWay, false, false, true, 1000000);
else
   $cat_products = $category->getProducts(intval($cookie->id_lang), intval($p), intval($n), $orderBy, $orderWay);            



This code will randomly choose up to 1000000 products from the category when the sort option is "--". When any other sort option is selected, the products are not sorted randomly. The reason I chose 1000000 is that there is no "unlimited" parameter, so I chose a number that is unlikely to be exceeded. Further code modifications would be required to make it truly unlimited.

Link to comment
Share on other sites

  • 2 months later...

Hi!

I would need this function, but it didn't work for me. I think because I use a filter module. (Filter Search Community Edition v2.1)

I think modifications needs in product-list.tpl (I don't know, because I'm not a programmer..)

product-list.tpl:



{if isset($products)}
<!-- Products list -->

<!-- /Products list -->
{/if}




Could somebody help me in that?


I use PS 1.2.5 Stable
my page: http://chiks.hu


Thanks!!!



The easiest way is to change line 59 of category.php (in Prestashop v1.2.5) from:

$cat_products = $category->getProducts(intval($cookie->id_lang), intval($p), intval($n), $orderBy, $orderWay);



to:

if ($orderBy == 'position')
   $cat_products = $category->getProducts(intval($cookie->id_lang), intval($p), intval($n), $orderBy, $orderWay, false, false, true, 1000000);
else
   $cat_products = $category->getProducts(intval($cookie->id_lang), intval($p), intval($n), $orderBy, $orderWay);            



This code will randomly choose up to 1000000 products from the category when the sort option is "--". When any other sort option is selected, the products are not sorted randomly. The reason I chose 1000000 is that there is no "unlimited" parameter, so I chose a number that is unlikely to be exceeded. Further code modifications would be required to make it truly unlimited.

Link to comment
Share on other sites

  • 7 months later...


This code will randomly choose up to 1000000 products from the category when the sort option is "--". When any other sort option is selected, the products are not sorted randomly. The reason I chose 1000000 is that there is no "unlimited" parameter, so I chose a number that is unlikely to be exceeded. Further code modifications would be required to make it truly unlimited.


I don't have this option "--" in version 1.3.3. So how can I improve this ?
Link to comment
Share on other sites

No, and also not in original presta-template. So I will add this now. Shall this option appear on BO at preferences > products as a value ? Because it is not there to choose.

It now appears on pagination after product-listing of my products in first row. On the row before I have the filter-options which customer can choose:

--
price: lower first
price: higher first
name: A to Z
name: Z to A
on stock first

What I want is that by each refresh the product-list shows not by default or other filter but products as random. Acutal presta version is 1.3.6.

Link to comment
Share on other sites

  • 3 weeks later...
  • 4 weeks later...

I was looking to order categories and subcategories as random, not products.

The project was a restaurant directory in Switzerland and they didn't want to be seen to promote any restaurant in particular, so they wanted the results to come up randomly.

I solved it by doing the following:

Open /classes/Category.php

Line 419, change
ORDER BY `level_depth` ASC, c.`position` ASC');
to
ORDER BY RAND();');

This seemed to do the trick!

I hope this helps anyone looking for this.

Link to comment
Share on other sites

I was looking to order categories and subcategories as random, not products.

The project was a restaurant directory in Switzerland and they didn't want to be seen to promote any restaurant in particular, so they wanted the results to come up randomly.

I solved it by doing the following:

Open /classes/Category.php

Line 419, change
ORDER BY `level_depth` ASC, c.`position` ASC');
to
ORDER BY RAND();');

This seemed to do the trick!

I hope this helps anyone looking for this.


does not work for me.... It delivers a 500 site... In which prestaversion did you added ?
Link to comment
Share on other sites

Ah, the formatting when posting messes it up.

Let me try that again.

ORDER BY RAND();');



Even this is wrong when I use tag.

Basically you need to write ORDER BY RAND then with no space afterwards open and close brackets, semicolon, close single quote, close bracket then another semicolon. All with no space after RAND.

I hope this helps :P

I'm sure there's a better way but I'm not much of a coder

Link to comment
Share on other sites

Ah, the formatting when posting messes it up.

Basically you need to write ORDER BY RAND then with no space afterwards open and close brackets, semicolon, close single quote, close bracket then another semicolon. All with no space after RAND.



Hi this one works, but no random for me...
Link to comment
Share on other sites

Same problem and same code. What should it be between the ; and ) An apostrophe, or an accent ?


Ok sorry about this folks, seems there's no way to add an apostrophe to these posts. The apostrophe should be between the (); and );
Again with no spaces.
Link to comment
Share on other sites

I added an apostrophe, but it is no random. I changed also on BO the pereferences into Positions into category. Also no random for me. Perhaps IIS don't understand the code ? I don't know. The only random what I got working is on tag block.

Link to comment
Share on other sites

  • 2 years later...
  • 3 weeks later...
  • 1 year later...

I found the way to display random products in category page on PS 1.6.2
Go to classes>Category.php
 
In the line 688
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)
 
change "$random = false" to "$random = true"

and set $random_number_products = 1 to the number of products you want to show in random.

 

In my store I've set to display 80 products per page, so I've set $random_number_products = 1 to $random_number_products = 80.

 

Note:  If you set the random number of products to large number, page loading time increase. Try to put the random to lowest possible.

  • Like 2
Link to comment
Share on other sites

  • 1 month later...
  • 4 months later...

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?

 

Using PS V 1.5.6.2

 

Thanks!

Link to comment
Share on other sites

  • 11 months later...

Same for me - doesn't work - please help!

 

The only thing that partly worked is :

 

shuffle($products)

 

added to product-list.tpl - but it shuffles product within one product page (so if I have 12 prods - they're displayed random) - I want all the products within category to shuffle 

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...