Maxi Videojuegos Posted April 28, 2023 Share Posted April 28, 2023 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 More sharing options...
qphoria Posted May 5, 2023 Share Posted May 5, 2023 (edited) 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 May 5, 2023 by qphoria (see edit history) 1 Link to comment Share on other sites More sharing options...
Maxi Videojuegos Posted May 5, 2023 Author Share Posted May 5, 2023 Hi qphoria! THANK YOU VERY MUCH!!! I copied the lines of code you wrote and followed your instructions and it worked perfectly! I really appreciate your help!☺️ Best regards! Link to comment Share on other sites More sharing options...
qphoria Posted May 5, 2023 Share Posted May 5, 2023 No worries mate, I am glad it works 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