Jump to content
blingblingpow

random products on category page

Recommended Posts

Is it possible to get random products on the main category page?

For example:
Category - Cheese
Products are listed in the order they were added.

I want to change it so that all the products are listed in a random order.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites


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 ?

Share this post


Link to post
Share on other sites

Do you have the following on line 15 of pagination.tpl in your theme's directory?

{l s='--'}



This is the -- option I mentioned.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

You should have the following options on the Prefrences > Products tab for "Default order by":

Product name
Product price
Product added date
Position inside category
Manufacturer
Product modified date

Share this post


Link to post
Share on other sites

Rocky thank you very much, works perfectly in prestashop 1.3.6 !
Greetings

Share this post


Link to post
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.

Share this post


Link to post
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 ?

Share this post


Link to post
Share on other sites
does not work for me…. It delivers a 500 site…


My bad, didn't write it properly. Testament to the value of copy&paste;:P

replace with this:
ORDER BY RAND();');

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

post overlapped, also the


ORDER BY RAND(););

not working and ending in 500.

Share this post


Link to post
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...

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

How can we do this on newest prestashop version 1.5.x ? I really need products to be listed randomly in all categories and front page. 

 

Please help

Share this post


Link to post
Share on other sites

How can we do this on newest prestashop version 1.5.x ? I really need products to be listed randomly in all categories and front page. 

 

Please help

+1

also want to know how to display randomly goods category page?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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 

Share this post


Link to post
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...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More