Jump to content

Edit History

qphoria

qphoria

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/

qphoria

qphoria

Hi @Maxi Videojuegos,

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

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.

 

×
×
  • Create New...