Jump to content

Changing sorting order in Specials Block Module


Maxi Videojuegos

Recommended Posts

Hello!

I've been trying to change the order the items show within the Specials Block but unfortunately they only show from higher to lower price.

Is there a way to sort them randomly? I've been messing around with the ps_specials.php but nothing seems to work.

Any help would be gladly appreciated,

 

Regards!

 

Max

Link to comment
Share on other sites

Hi @Maxi Videojuegos,

I was trying to achieve the same i.e. ordering specials (discounted) products ramdomly on the homepage and managed to make it work.

You can randomize the array of products in PHP after they were queried from the database. To do this, find the getSpecialProducts private function in either modules/ps_specials.php or in the specials file provided by your theme (in my case, I am using the AngarTheme so I had to edit the modules/angarspecials.php file) and add these lines of code:

// Find this line of code
$products = Product::getPricesDrop(...);

// add the lines below to shuffle the array of products coming from the SQL query

// /!\ clearing the cache otherwise templates will keep on rendering products in the same order, even if we reshuffled them
$this->_clearCache('*');

// here we reshuffle the keys of the $products array, and use a $temp array to store the new list of products
$temp = array();
$keys = array_keys($products);
shuffle($keys);

foreach ($keys as $key)
{
   $temp[$key] = $products[$key];
}

// and eventually give the new list of products to the $products array
$products = $temp;

I am quite new to PrestaShop so I am not sure if this is the best way to achieve it, but it works :)

Let me know how it goes.

Here's the link to my store where you can see the list of products being randomly displayed whenever you refresh the page: https://www.inox-horeca-pro.com/fr/

Edited by qphoria (see edit history)
  • Thanks 1
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...