Jump to content
rakepl

Random product display on featured page

Recommended Posts

For another visiting or refreshing page (from one ip or different ip) site shows different product on featured products page. Please give any hint or module that can do that ?

Thank you.

Share this post


Link to post
Share on other sites

Add

shuffle($products);

in your homefeatured.php file at line 100 which should make it look something like

function hookHome($params)
{
global $smarty;

$category = new Category(1, Configuration::get('PS_LANG_DEFAULT'));
$nb = (int)(Configuration::get('HOME_FEATURED_NBR'));
$products = $category->getProducts((int)($params['cookie']->id_lang), 1, ($nb ? $nb : 10));
shuffle($products);
$smarty->assign(array(
'products' => $products,
'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),
'homeSize' => Image::getSize('home')));

return $this->display(__FILE__, 'homefeatured.tpl');

  • Like 13

Share this post


Link to post
Share on other sites

WOWOW,

Thank you very much, it's work !! We need person in this forum like you :)

Where I can learn some stuffs like that ?

Once again, thank you !

Share this post


Link to post
Share on other sites

Hi jhnstcks!

 

By chance would you happen to know how to assign products as ‘featured products’ so they display on the homepage?

 

Art

Share this post


Link to post
Share on other sites

Thanks! That was very useful. I probably would have been trying to figure this out for the next 12 hours before giving up to go back to sleep. You're a life saver jhnstcks!

Share this post


Link to post
Share on other sites

Better is to add this code,

 

$products = $category->getProducts(intval($params['cookie']->id_lang), 1, 100);
if ($products) {
shuffle($products);
array_splice($products, ($nb ? $nb : 10));
}

 

Cause the above posted by jhnstcks shows only random at home and place it only at a different position ( every time the same products) the code posted will do the following.

 

Lets say i have 12 items to show add home that you define in the home featured module, but i add 50 products in category and also marked home then it will randomly shows all 50 products and not only the 12 as you will have with jhnstcks code.

 

Great thanks for this useful code goes to IWAN1979 website with code can be found here http://iwanmadan.com/prestashop-

random-product-listing/

 

Regards,

 

ysco..

Edited by ysco (see edit history)
  • Like 8

Share this post


Link to post
Share on other sites

Dear Friend,

 

I need help from you to configure my store. I am facing issue related to home featured module. I have set 16 product to be shown randomly on homepage. But products are not appearing properly on page. It will be great if you help me to resolve it. It will be great if you help me.

 

My site url is

 

Many thanks in advance.

 

 

Regards

Edited by Vilas (see edit history)

Share this post


Link to post
Share on other sites

Hello everyone !

Is there a solution to randomize the same module in 1.5 ?

 

Thank you.

Share this post


Link to post
Share on other sites

I want to display different products for different countries using IP. Is it possible or any module available. Please help me with this.

Share this post


Link to post
Share on other sites

Better is to add this code,

 

$products = $category->getProducts(intval($params['cookie']->id_lang), 1, 100);
if ($products) {
shuffle($products);
array_splice($products, ($nb ? $nb : 10));
}

 

Cause the above posted by jhnstcks shows only random at home and place it only at a different position ( every time the same products) the code posted will do the following.

 

Lets say i have 12 items to show add home that you define in the home featured module, but i add 50 products in category and also marked home then it will randomly shows all 50 products and not only the 12 as you will have with jhnstcks code.

 

Great thanks for this useful code goes to IWAN1979 website with code can be found here http://iwanmadan.com/prestashop-

random-product-listing/

 

Regards,

 

ysco..

 

Tested on PS 1.5 and work fiine

  • Like 4

Share this post


Link to post
Share on other sites

in this way, however, are mixed only the first products.

I would like to randomly mix all products in the database

Share this post


Link to post
Share on other sites

Better is to add this code,

 

$products = $category->getProducts(intval($params['cookie']->id_lang), 1, 100);
if ($products) {
shuffle($products);
array_splice($products, ($nb ? $nb : 10));
}

 

Cause the above posted by jhnstcks shows only random at home and place it only at a different position ( every time the same products) the code posted will do the following.

 

Lets say i have 12 items to show add home that you define in the home featured module, but i add 50 products in category and also marked home then it will randomly shows all 50 products and not only the 12 as you will have with jhnstcks code.

 

Great thanks for this useful code goes to IWAN1979 website with code can be found here http://iwanmadan.com/prestashop-

random-product-listing/

 

Regards,

 

ysco..

 

Works perfectly in 1.5.2

Thanks

Share this post


Link to post
Share on other sites

Is this change then correct? Or did I enter it at the wrong place?

function hookHome($params)
{
 global $smarty;
 $category = new Category(1, (int)Configuration::get('PS_LANG_DEFAULT'));
 $nb = (int)Configuration::get('HOME_FEATURED_NBR');
/*ITO SHUFFLE right place herE?*/
    $products = $category->getProducts(intval($params['cookie']->id_lang), 1, 100);
    if ($products) {
    shuffle($products);
    array_splice($products, ($nb ? $nb : 10));
    }

 $smarty->assign(array(
 'products' => $category->getProducts((int)$params['cookie']->id_lang, 1, ($nb ? $nb : 10)),
 'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),
 'homeSize' => Image::getSize('home')));
 return $this->display(__FILE__, 'homefeatured.tpl');
}

Edited by B.Köring (see edit history)

Share this post


Link to post
Share on other sites

Yes this works in 1.5.3.1 too!

 

Eveh with changed reference category (I changed it to 888 see below).

It is all very nice this way.

 

But when I change languages, no featured products are found to display on the homepage.

 

Does anyone here know how to make the featured products appear on the omepages in other languages than the default language?

 

 

This is the resume of the code I using the info from the posts above (thank you all for the input):

 

public function hookDisplayHome($params)

{

$category = new Category(888, Context::getContext()->shop->getCategory(), (int)Context::getContext()->language->id);

$nb = (int)(Configuration::get('HOME_FEATURED_NBR'));

$products = $category->getProducts(intval($params['cookie']->id_lang), 1, 5000);

if ($products) {

shuffle($products);

array_splice($products, ($nb ? $nb : 10));

}

$this->smarty->assign(array(

'products' => $products,

'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),

'homeSize' => Image::getSize(ImageType::getFormatedName('home')),

));

 

return $this->display(__FILE__, 'homefeatured.tpl');

}

 

==================

 

 

 

 

But, if I change languages from the default EN to NL,

no featured products are displayed on the homepage anymore!

 

Does someone know a solution for that?

Don't have a clue

Share this post


Link to post
Share on other sites

Add

shuffle($products);

in your homefeatured.php file at line 100 which should make it look something like

function hookHome($params)
{
global $smarty;

$category = new Category(1, Configuration::get('PS_LANG_DEFAULT'));
$nb = (int)(Configuration::get('HOME_FEATURED_NBR'));
$products = $category->getProducts((int)($params['cookie']->id_lang), 1, ($nb ? $nb : 10));
shuffle($products);
$smarty->assign(array(
'products' => $products,
'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),
'homeSize' => Image::getSize('home')));

return $this->display(__FILE__, 'homefeatured.tpl');

 

Hi everyone,

 

anyone knows in which class I can find the getProducts function?

I need to duplicate it and add a filter.

 

Thanks in advance!

Patrizia

Share this post


Link to post
Share on other sites

Do you get code for presta 1.4.6.2 ????? Code

function hookHome($params)

{

global $smarty;

 

$category = new Category(1, Configuration::get('PS_LANG_DEFAULT'));

$nb = (int)(Configuration::get('HOME_FEATURED_NBR'));

$products = $category->getProducts((int)($params['cookie']->id_lang), 1, ($nb ? $nb : 10));

shuffle($products);

$smarty->assign(array(

'products' => $products,

'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),

'homeSize' => Image::getSize('home')));

 

return $this->display(__FILE__, 'homefeatured.tpl');

is good but don´t show randomly products.............

a this code for version 1.4.6.2 is not compatible......

$products = $category->getProducts(intval($params['cookie']->id_lang), 1, 100);

if ($products) {

shuffle($products);

array_splice($products, ($nb ? $nb : 10));

}

Share this post


Link to post
Share on other sites

Works fine in 1.5.3.

 

Kashibabu, what about small change like this:

 

 

$products = $category->getProducts((int)Context::getContext()->language->id, 1, 100);

if ($products) {shuffle($products);array_splice($products, ($nb ? $nb : 10));

 

Languages seem to work fine.

Share this post


Link to post
Share on other sites

Better is to add this code,

 

$products = $category->getProducts(intval($params['cookie']->id_lang), 1, 100);
if ($products) {
shuffle($products);
array_splice($products, ($nb ? $nb : 10));
}

 

Cause the above posted by jhnstcks shows only random at home and place it only at a different position ( every time the same products) the code posted will do the following.

 

Lets say i have 12 items to show add home that you define in the home featured module, but i add 50 products in category and also marked home then it will randomly shows all 50 products and not only the 12 as you will have with jhnstcks code.

 

Great thanks for this useful code goes to IWAN1979 website with code can be found here http://iwanmadan.com/prestashop-

random-product-listing/

 

Regards,

 

ysco..

Works for 1.4.7.3 !

Share this post


Link to post
Share on other sites

Hey, could something like this work for the Specials block? I'd like to try it but have no idea where to put any code...

Share this post


Link to post
Share on other sites

you guys ROCK! i stumbled upon this and implemented it in 1.5.3 and it is AWESOME so far.

 

kashibabu, i have my site in spanish and english for now (more langs to come) and the original code posted by "ysco" a page back works for me in both languages. you can see it working at http://www.fixgear.info .

 

ysco, so, basically, your code will pull randomly from any products i associate with the "home" category?

you see, i am adding products now to my store and so far have only added in one category so i have no way to test until i add to more categories.

 

awesome so far though!!!

 

saludos,

jer...

Share this post


Link to post
Share on other sites

BTW, ysco, i have a question.

 

i have tweaked the "accessories" tab on my product pages to show related products instead since my products do not have accessories.

 

could i apply your code to that section so the related products shuffle like that? if so, what code would i paste in and where?

 

see http://www.fixgear.info/index.php?id_product=13&controller=product&id_lang=1 for what i mean. just look at the tabs beneath the product and click the "Related Items" tab.

 

thanks!!!

jer...

Share this post


Link to post
Share on other sites

hmmm... now that i think about it, i would not want ALL products to shuffle on a category page so i think that the code by jhnstcks would work better but CAN IT BE APPLIED TO category pages that list the products in that category so they shuffle each time the page loads?

 

anyone?

 

in what file and on what line would the code go?

 

thanks,

jer...

Share this post


Link to post
Share on other sites

can you help me with my featured products?

I purchased a template from Templatemonster.com and I have another file, (prestashop 1.5.3.1)

 

if (!defined('_PS_VERSION_'))
exit;
class HomeFeatured extends Module
{
private $_html = '';
private $_postErrors = array();
function __construct()
{
 $this->name = 'homefeatured';
 $this->tab = 'front_office_features';
 $this->version = '0.9';
 $this->author = 'PrestaShop';
 $this->need_instance = 0;
 parent::__construct();
 $this->displayName = $this->l('Featured Products on the homepage');
 $this->description = $this->l('Displays Featured Products in the middle of your homepage.');
}
function install()
{
 if (!Configuration::updateValue('HOME_FEATURED_NBR', 8) || !parent::install() || !$this->registerHook('displayHome') || !$this->registerHook('header'))
  return false;
 return true;
}
public function getContent()
{
 $output = '<h2>'.$this->displayName.'</h2>';
 if (Tools::isSubmit('submitHomeFeatured'))
 {
  $nbr = (int)(Tools::getValue('nbr'));
  if (!$nbr OR $nbr <= 0 OR !Validate::isInt($nbr))
   $errors[] = $this->l('Invalid number of products');
  else
   Configuration::updateValue('HOME_FEATURED_NBR', (int)($nbr));
  if (isset($errors) AND sizeof($errors))
   $output .= $this->displayError(implode('<br />', $errors));
  else
   $output .= $this->displayConfirmation($this->l('Settings updated'));
 }
 return $output.$this->displayForm();
}
public function displayForm()
{
 $output = '
 <form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post">
  <fieldset><legend><img src="'.$this->_path.'logo.gif" alt="" title="" />'.$this->l('Settings').'</legend>
   <p>'.$this->l('In order to add products to your homepage, just add them to the "home" category.').'</p><br />
   <label>'.$this->l('Number of products displayed').'</label>
   <div class="margin-form">
 <input type="text" size="5" name="nbr" value="'.Tools::safeOutput(Tools::getValue('nbr', (int)(Configuration::get('HOME_FEATURED_NBR')))).'" />
 <p class="clear">'.$this->l('The number of products displayed on homepage (default: 10).').'</p>
   </div>
   <center><input type="submit" name="submitHomeFeatured" value="'.$this->l('Save').'" class="button" /></center>
  </fieldset>
 </form>';
 return $output;
}
public function hookDisplayHeader($params)
{
 $this->hookHeader($params);
}
public function hookHeader($params)
{
 $this->context->controller->addCss($this->_path.'homefeatured.css', 'all');
}
public function hookDisplayHome($params)
{
 $category = new Category(Context::getContext()->shop->getCategory(), (int)Context::getContext()->language->id);
 $nb = (int)(Configuration::get('HOME_FEATURED_NBR'));
 $products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 10));
 $this->smarty->assign(array(
  'products' => $products,
  'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),
  'homeSize' => Image::getSize(ImageType::getFormatedName('home')),
 ));
 return $this->display(__FILE__, 'homefeatured.tpl');
}
}

Share this post


Link to post
Share on other sites
can you help me with my featured products? I purchased a template from Templatemonster.com and I have another file, (prestashop 1.5.3.1)
 if (!defined('_PS_VERSION_')) exit; class HomeFeatured extends Module { private $_html = ''; private $_postErrors = array(); function __construct() { $this->name = 'homefeatured'; $this->tab = 'front_office_features'; $this->version = '0.9'; $this->author = 'PrestaShop'; $this->need_instance = 0; parent::__construct(); $this->displayName = $this->l('Featured Products on the homepage'); $this->description = $this->l('Displays Featured Products in the middle of your homepage.'); } function install() { if (!Configuration::updateValue('HOME_FEATURED_NBR', 8) || !parent::install() || !$this->registerHook('displayHome') || !$this->registerHook('header')) return false; return true; } public function getContent() { $output = '
[b]	'.$this->displayName.'[/b]

'; if (Tools::isSubmit('submitHomeFeatured')) { $nbr = (int)(Tools::getValue('nbr')); if (!$nbr OR $nbr <= 0 OR !Validate::isInt($nbr)) $errors[] = $this->l('Invalid number of products'); else Configuration::updateValue('HOME_FEATURED_NBR', (int)($nbr)); if (isset($errors) AND sizeof($errors)) $output .= $this->displayError(implode('
', $errors)); else $output .= $this->displayConfirmation($this->l('Settings updated')); } return $output.$this->displayForm(); } public function displayForm() { $output = '			[img='.$this->_path.]'.$this->l('Settings').'		
'.$this->l('In order to add products to your homepage, just add them to the "home" category.').'		
	'.$this->l('Number of products displayed').'		

'.$this->l('The number of products displayed on homepage (default: 10).').'								
'; return $output; } public function hookDisplayHeader($params) { $this->hookHeader($params); } public function hookHeader($params) { $this->context->controller->addCss($this->_path.'homefeatured.css', 'all'); } public function hookDisplayHome($params) { $category = new Category(Context::getContext()->shop->getCategory(), (int)Context::getContext()->language->id); $nb = (int)(Configuration::get('HOME_FEATURED_NBR')); $products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 10)); $this->smarty->assign(array( 'products' => $products, 'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'), 'homeSize' => Image::getSize(ImageType::getFormatedName('home')), )); return $this->display(__FILE__, 'homefeatured.tpl'); } } 

Between (bottom):

$products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 10));
 $this->smarty->assign(array(

Add:

$products = $category->getProducts(intval($params['cookie']->id_lang), 1, 100);
if ($products) {
shuffle($products);
array_splice($products, ($nb ? $nb : 10));
}

It'll be:

$products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 10));
	$products = $category->getProducts(intval($params['cookie']->id_lang), 1, 100);
	if ($products) {
	shuffle($products);
	array_splice($products, ($nb ? $nb : 10));
	}
	$this->smarty->assign(array(

That's all

Edited by ano (see edit history)
  • Like 1

Share this post


Link to post
Share on other sites

Hi,

 

I've slightly modified the Home Featured module to include the randomised option for folks that don't wish to modify the php code and also make it easier to disable the random selection if required.

 

In the configure option for the home featured module I've add an option to select the amount of products to randomly pick from.

 

If you set this to say 100, you featured products page will grab the first 100 products from your home category, shuffle them up and then pick out the amount of products that you have set to be shown.

 

To easily replace you existing module, simply go into the admin area, select the modules tab, locate the existing Home Featured module and select delete.

 

You then select Add a new module, click the browse button and point to the homefeatured.zip file that you have download (homefeatured.zip).

 

Install it by clicking install and then click configure.

 

In the configuration simply set the amount of products you wish to be displayed on the featured area as usual and then set the amount of products you wish to randomly pick from.

 

If you set this to 0 then the random feature will be disabled and you will be back to the standard functionality of the original Home Featured module.

 

Hope this helps... :)

  • Like 5

Share this post


Link to post
Share on other sites

hi all. i was wondering why only a limited amount of products were in the "pool" of products to be chosen randomly when i had all of my products selected to the "Home" category but i figured it out so i wanted to share it here.

 

change the "100" in...

 

$products = $category->getProducts(intval($params['cookie']->id_lang), 1, 100);

 

to the amt. of products you want to be candidates to appear on the home featured. if you leave it at 100 you limit that to the first 100 active products that you have added to your site.

 

i changed mine to 400 to include all of my products.

 

saludos,

jez...

Edited by fixgear (see edit history)

Share this post


Link to post
Share on other sites

Hi,

 

I've slightly modified the Home Featured module to include the randomised option for folks that don't wish to modify the php code and also make it easier to disable the random selection if required.

 

In the configure option for the home featured module I've add an option to select the amount of products to randomly pick from.

 

If you set this to say 100, you featured products page will grab the first 100 products from your home category, shuffle them up and then pick out the amount of products that you have set to be shown.

 

To easily replace you existing module, simply go into the admin area, select the modules tab, locate the existing Home Featured module and select delete.

 

You then select Add a new module, click the browse button and point to the homefeatured.zip file that you have download (homefeatured.zip).

 

Install it by clicking install and then click configure.

 

In the configuration simply set the amount of products you wish to be displayed on the featured area as usual and then set the amount of products you wish to randomly pick from.

 

If you set this to 0 then the random feature will be disabled and you will be back to the standard functionality of the original Home Featured module.

 

Hope this helps... :)

 

 

Works in 1.5.4.1 :-) Thank a lot !

  • Like 1

Share this post


Link to post
Share on other sites

Hi,

 

I've slightly modified the Home Featured module to include the randomised option for folks that don't wish to modify the php code and also make it easier to disable the random selection if required.

 

In the configure option for the home featured module I've add an option to select the amount of products to randomly pick from.

 

If you set this to say 100, you featured products page will grab the first 100 products from your home category, shuffle them up and then pick out the amount of products that you have set to be shown.

 

To easily replace you existing module, simply go into the admin area, select the modules tab, locate the existing Home Featured module and select delete.

 

You then select Add a new module, click the browse button and point to the homefeatured.zip file that you have download (homefeatured.zip).

 

Install it by clicking install and then click configure.

 

In the configuration simply set the amount of products you wish to be displayed on the featured area as usual and then set the amount of products you wish to randomly pick from.

 

If you set this to 0 then the random feature will be disabled and you will be back to the standard functionality of the original Home Featured module.

 

Hope this helps... :)

 

 

I try in 1.5.3.1 the module it works in all browsers,(chrome- firefox - safari.) but not in IE from 8 until 10 version.they dint show the photos of the products.Any solution for this bug for me. it happen and in others ?? or only to me. Thanks

  • Like 2

Share this post


Link to post
Share on other sites

I try in 1.5.3.1 the module it works in all browsers,(chrome- firefox - safari.) but not in IE from 8 until 10 version.they dint show the photos of the products.Any solution for this bug for me. it happen and in others ?? or only to me. Thanks

 

very good ... thanks

  • Like 1

Share this post


Link to post
Share on other sites

Add

shuffle($products);

in your homefeatured.php file at line 100 which should make it look something like

function hookHome($params)
{
global $smarty;

$category = new Category(1, Configuration::get('PS_LANG_DEFAULT'));
$nb = (int)(Configuration::get('HOME_FEATURED_NBR'));
$products = $category->getProducts((int)($params['cookie']->id_lang), 1, ($nb ? $nb : 10));
shuffle($products);
$smarty->assign(array(
'products' => $products,
'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),
'homeSize' => Image::getSize('home')));

return $this->display(__FILE__, 'homefeatured.tpl');

Thank you!!!

Share this post


Link to post
Share on other sites

$products = $category->getProducts(intval($params['cookie']->id_lang), 1, 100);
if ($products) {
shuffle($products);
array_splice($products, ($nb ? $nb : 10));
}

 

Was useing this on 1.5.4.1 it worked fine no longer works in 1.5.5 Help please

 

Thanks

Jeff

Share this post


Link to post
Share on other sites

Don't work for me in 1.5.5 

zip.gif  homefeatured.zip   10.52KB   92 downloads).

 

:(

 

you've got some error messages ? if so, can you share them?

Share this post


Link to post
Share on other sites

There is no different when you call some products on Prestashop 1.5.5. Please upload some screenshots for more informations

Edited by WiaXi (see edit history)

Share this post


Link to post
Share on other sites

Hi,

 

I've slightly modified the Home Featured module to include the randomised option for folks that don't wish to modify the php code and also make it easier to disable the random selection if required.

 

In the configure option for the home featured module I've add an option to select the amount of products to randomly pick from.

 

If you set this to say 100, you featured products page will grab the first 100 products from your home category, shuffle them up and then pick out the amount of products that you have set to be shown.

 

To easily replace you existing module, simply go into the admin area, select the modules tab, locate the existing Home Featured module and select delete.

 

You then select Add a new module, click the browse button and point to the homefeatured.zip file that you have download (attachicon.gifhomefeatured.zip).

 

Install it by clicking install and then click configure.

 

In the configuration simply set the amount of products you wish to be displayed on the featured area as usual and then set the amount of products you wish to randomly pick from.

 

If you set this to 0 then the random feature will be disabled and you will be back to the standard functionality of the original Home Featured module.

 

Hope this helps... :)

 

Thank you very much. Nice Guy!!!

Share this post


Link to post
Share on other sites

I have one other Problem... If i use the Cache System, it will not refresh random - only i delete Smarty Cache by Hand.

Is there a Way to avoid this random Function from cache?

regards Sven

Share this post


Link to post
Share on other sites

just don't use cache for homefeatured module, you can modify .php file and remove cache functions

Share this post


Link to post
Share on other sites

just don't use cache for homefeatured module, you can modify .php file and remove cache functions

 

Hi Vekia,

 

Can you tell me how to disable cache functions for just this module ?

 

Thanks

Share this post


Link to post
Share on other sites

remove all $this->_clearCache('homefeatured.tpl'); instances from .php file

 

also remove it from hookDisplayHome($params) function:

if (!$this->isCached('homefeatured.tpl', $this->getCacheId('homefeatured'))){

}

Share this post


Link to post
Share on other sites

hi, would it be possible to randomize, for example the "block new products" or "last viewed" ? (adding this code in those modules result in errors)

V 1.5.4

Share this post


Link to post
Share on other sites

I uploaded the module, but it does not show anywhere. It shows the module was uploaded successfully, but it does not appear in the list. What should i do? i am using 1.5 thx

Share this post


Link to post
Share on other sites

Hello, I would get random products within categories.

I use prestashop version 1.4.10 and I do not work to change the code in the productcategory.php file (which I see if it works in other versions):
$ categoryProducts = $ category-> getProducts (intval ($ params ['cookie'] -> id_lang), 1, 100);
if ($ categoryProducts) {
shuffle ($ categoryProducts);
array_splice ($ categoryProducts, ($ nb $ nb: 10));
}

I would comment that I temporarily deleted productcategory.php and continues to show me the list. It's as if the file had nothing to do.
Where I can be wrong?

Greetings and thanks. (and sorry for my english google)

Share this post


Link to post
Share on other sites

remove all $this->_clearCache('homefeatured.tpl'); instances from .php file

 

also remove it from hookDisplayHome($params) function:

if (!$this->isCached('homefeatured.tpl', $this->getCacheId('homefeatured'))){

}

 

$this->_clearCache('homefeatured.tpl');

this apears in many lines. wich one to remove?

 

and from the hook, wich line also?

 

thanks.

 

 

if (!defined('_PS_VERSION_'))

    exit;

 

class HomeFeatured extends Module

{

    private $_html = '';

    private $_postErrors = array();

 

    function __construct()

    {

        $this->name = 'homefeatured';

        $this->tab = 'front_office_features';

        $this->version = '1.1';

        $this->author = 'PrestaShop';

        $this->need_instance = 0;

 

        parent::__construct();

 

        $this->displayName = $this->l('Featured products on the homepage.');

        $this->description = $this->l('Displays featured products in the middle of your homepage.');

    }

 

    function install()

    {

        $this->_clearCache('homefeatured.tpl');

        Configuration::updateValue('HOME_FEATURED_NBR', 8);

 

        if (!parent::install()

            || !$this->registerHook('displayHome')

            || !$this->registerHook('header')

            || !$this->registerHook('addproduct')

            || !$this->registerHook('updateproduct')

            || !$this->registerHook('deleteproduct')

        )

            return false;

        return true;

    }

    

    public function uninstall()

    {

        $this->_clearCache('homefeatured.tpl');

        return parent::uninstall();

    }

 

    public function getContent()

    {

        $output = '<h2>'.$this->displayName.'</h2>';

        if (Tools::isSubmit('submitHomeFeatured'))

        {

            $nbr = (int)Tools::getValue('nbr');

            if (!$nbr OR $nbr <= 0 OR !Validate::isInt($nbr))

                $errors[] = $this->l('An invalid number of products has been specified.');

            else

                Configuration::updateValue('HOME_FEATURED_NBR', (int)($nbr));

            if (isset($errors) AND sizeof($errors))

                $output .= $this->displayError(implode('<br />', $errors));

            else

                $output .= $this->displayConfirmation($this->l('Your settings have been updated.'));

        }

        return $output.$this->displayForm();

    }

 

    public function displayForm()

    {

        $output = '

        <form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post">

            <fieldset><legend><img src="'.$this->_path.'logo.gif" alt="" title="" />'.$this->l('Settings').'</legend>

                <p>'.$this->l('To add products to your homepage, simply add them to the "home" category.').'</p><br />

                <label>'.$this->l('Define the number of products to be displayed.').'</label>

                <div class="margin-form">

                    <input type="text" size="5" name="nbr" value="'.Tools::safeOutput(Tools::getValue('nbr', (int)(Configuration::get('HOME_FEATURED_NBR')))).'" />

                    <p class="clear">'.$this->l('Define the number of products that you would like to display on homepage (default: 8).').'</p>

 

                </div>

                <center><input type="submit" name="submitHomeFeatured" value="'.$this->l('Save').'" class="button" /></center>

            </fieldset>

        </form>';

        return $output;

    }

 

    public function hookDisplayHeader($params)

    {

        $this->hookHeader($params);

    }

 

    public function hookHeader($params)

    {

        $this->context->controller->addCSS(($this->_path).'homefeatured.css', 'all');

    }

 

    public function hookDisplayHome($params)

    {

        if (!$this->isCached('homefeatured.tpl', $this->getCacheId('homefeatured')))

        {

            $category = new Category(Context::getContext()->shop->getCategory(), (int)Context::getContext()->language->id);

            $nb = (int)Configuration::get('HOME_FEATURED_NBR');

            $products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), "position");;

            

$this->smarty->assign(array(

                'products' => $products,

                'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),

                'homeSize' => Image::getSize(ImageType::getFormatedName('home')),

            ));

        }

        return $this->display(__FILE__, 'homefeatured.tpl', $this->getCacheId('homefeatured'));

    }

 

    public function hookAddProduct($params)

    {

        $this->_clearCache('homefeatured.tpl');

    }

 

    public function hookUpdateProduct($params)

    {

        $this->_clearCache('homefeatured.tpl');

    }

 

    public function hookDeleteProduct($params)

    {

        $this->_clearCache('homefeatured.tpl');

    }

}

Edited by adrianpasita (see edit history)

Share this post


Link to post
Share on other sites

you have to remove it from all available places in module php file

Share this post


Link to post
Share on other sites

you have to remove it from all available places in module php file

 

 

still not working.

I give up!

I”ve tried all this solution from this forum.

I use PS 1.5.6.2.

Share this post


Link to post
Share on other sites

if you use 1.5.6.2 you don't have to remove this code because prestashop 1.5.6.2 supports cache for modules

Share this post


Link to post
Share on other sites

Between (bottom):

$products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 10));
  $this->smarty->assign(array(
Add:

$products = $category->getProducts(intval($params['cookie']->id_lang), 1, 100);
if ($products) {
shuffle($products);
array_splice($products, ($nb ? $nb : 10));
}
It'll be:

$products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 10));
		$products = $category->getProducts(intval($params['cookie']->id_lang), 1, 100);
		if ($products) {
		shuffle($products);
		array_splice($products, ($nb ? $nb : 10));
		}
		$this->smarty->assign(array(
That's all

 

Works on 1.5.4.1. Thanks

Edited by b22c (see edit history)

Share this post


Link to post
Share on other sites

I can't put running on 1.5.6.2 any help?

 

I have the same problem.

I tried all the solution on this forum and it dosent work.

Share this post


Link to post
Share on other sites

method two posts above should work for you. 

make sure that after changes you cleared cache and force compiled theme,

without this you will not see changes

  • Like 1

Share this post


Link to post
Share on other sites

strange,

you've got enough products attached to home category?

Share this post


Link to post
Share on other sites

strange,

you've got enough products attached to home category?

 

All my products are in different subcategories.

There are checked on Home also, but i setted Default category the subcategorie.

 

Is that the problem?

Share this post


Link to post
Share on other sites

homefeatured module uses products associated with "home" category

if you want to dispaly products in featured block - you have to associate products with this category.

Share this post


Link to post
Share on other sites

homefeatured module uses products associated with "home" category

if you want to dispaly products in featured block - you have to associate products with this category.

 

How this can be done?

Share this post


Link to post
Share on other sites

go to product edit page, and associate it with "home" category

Share this post


Link to post
Share on other sites

if you use 1.5.6.2 you don't have to remove this code because prestashop 1.5.6.2 supports cache for modules

Hello im using 1.5.6.2.

im wondering how to disable caching for this module, to get fresh list of featured products for every refresh of page. 

Or any oher way to achieve this (if possible- cache 100 products and then pick random 7pcs every time page is reloaded)

Thank you :) <3

 

www.prosperk.cz

Edited by lalala lululu (see edit history)

Share this post


Link to post
Share on other sites

you can remove cache only with modification of module php file, there is no other way, unfortunately.

 

inside homefeatured.php file you can find code like:
 

	public function hookDisplayHome($params)
	{
		if (!$this->isCached('homefeatured.tpl', $this->getCacheId('homefeatured')))
		{
			$category = new Category(Context::getContext()->shop->getCategory(), (int)Context::getContext()->language->id);
			$nb = (int)Configuration::get('HOME_FEATURED_NBR');
			$products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), "position");

			$this->smarty->assign(array(
				'products' => $products,
				'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),
				'homeSize' => Image::getSize(ImageType::getFormatedName('home')),
			));
		}
		return $this->display(__FILE__, 'homefeatured.tpl', $this->getCacheId('homefeatured'));
	}

instead of this code, use this one:

	public function hookDisplayHome($params)
	{
		
			$category = new Category(Context::getContext()->shop->getCategory(), (int)Context::getContext()->language->id);
			$nb = (int)Configuration::get('HOME_FEATURED_NBR');
			$products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), "position");

			$this->smarty->assign(array(
				'products' => $products,
				'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),
				'homeSize' => Image::getSize(ImageType::getFormatedName('home')),
			));
		return $this->display(__FILE__, 'homefeatured.tpl');
	}
  • Like 1

Share this post


Link to post
Share on other sites

 

you can remove cache only with modification of module php file, there is no other way, unfortunately.

 

inside homefeatured.php file you can find code like:

 

	public function hookDisplayHome($params)
	{
		if (!$this->isCached('homefeatured.tpl', $this->getCacheId('homefeatured')))
		{
			$category = new Category(Context::getContext()->shop->getCategory(), (int)Context::getContext()->language->id);
			$nb = (int)Configuration::get('HOME_FEATURED_NBR');
			$products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), "position");

			$this->smarty->assign(array(
				'products' => $products,
				'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),
				'homeSize' => Image::getSize(ImageType::getFormatedName('home')),
			));
		}
		return $this->display(__FILE__, 'homefeatured.tpl', $this->getCacheId('homefeatured'));
	}

instead of this code, use this one:

	public function hookDisplayHome($params)
	{
		
			$category = new Category(Context::getContext()->shop->getCategory(), (int)Context::getContext()->language->id);
			$nb = (int)Configuration::get('HOME_FEATURED_NBR');
			$products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), "position");

			$this->smarty->assign(array(
				'products' => $products,
				'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),
				'homeSize' => Image::getSize(ImageType::getFormatedName('home')),
			));
		return $this->display(__FILE__, 'homefeatured.tpl');
	}

Thank you for your reply.

Unfortunately i already made this before i ask... but module still works like it uses cache... shuffeling works good, but i see change only after reloading my cache. I dont understand why. Im sure i removed the "isCached" condition as you said. prosperk.cz here you can see in block "Vybrali jsme pro Vás". Thank you again Vekia.

Share this post


Link to post
Share on other sites

and removed it also from return $this->display(__FILE__, 'homefeatured.tpl'); ?

Share this post


Link to post
Share on other sites

and removed it also from return $this->display(__FILE__, 'homefeatured.tpl'); ?

Ah thats it! Thank you for your dumb resistant patience. :)

Share this post


Link to post
Share on other sites

)so, now after changes, it works as it should ? :) (God, i hope so ;)

  • Like 1

Share this post


Link to post
Share on other sites

and removed it also from return $this->display(__FILE__, 'homefeatured.tpl'); ?

 

Can you be more specific please?

Share this post


Link to post
Share on other sites

by default code looks like:
 

return $this->display(__FILE__, 'homefeatured.tpl', $this->getCacheId('homefeatured'));

it's necessary to change it to

return $this->display(__FILE__, 'homefeatured.tpl');

i removed this part of code: 

, $this->getCacheId('homefeatured')

it's a part related to cache, so it's necessary to remove it.

  • Like 1

Share this post


Link to post
Share on other sites

Changing the code work for me and i use it for about 1 month but i note that some of my products were never displayed.

 

 

If i want to display lets say 28 products ramdomly i just check the category home, or i can displayed ramdomly by category... Very happy with this module... I note that some products that werent displayed with changing the code are now displayed...

  • Like 1

Share this post


Link to post
Share on other sites

Using PrestaShop Legend examples, my prestashop 1.5.6.2 is working as well. Thanks a lot
 

		public function hookDisplayHome($params) {
		
			$category = new Category(Context::getContext()->shop->getCategory(), (int)Context::getContext()->language->id);
			$nb = (int)Configuration::get('HOME_FEATURED_NBR');
			$products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), "position");
            
			if ($products) {
		        shuffle($products);
		        array_splice($products, ($nb ? $nb : 10));
		    }
			
			$this->smarty->assign(array(
				'products' => $products,
				'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),
				'homeSize' => Image::getSize(ImageType::getFormatedName('home')),
			));
		return $this->display(__FILE__, 'homefeatured.tpl');
	}

Sorry my bad english.

Edited by Cassiano Aquino (see edit history)
  • Like 3

Share this post


Link to post
Share on other sites

Using PrestaShop Legend examples, my prestashop 1.5.6.2 is working as well. Thanks a lot

 

		public function hookDisplayHome($params) {
		
			$category = new Category(Context::getContext()->shop->getCategory(), (int)Context::getContext()->language->id);
			$nb = (int)Configuration::get('HOME_FEATURED_NBR');
			$products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), "position");
            
			if ($products) {
		        shuffle($products);
		        array_splice($products, ($nb ? $nb : 10));
		    }
			
			$this->smarty->assign(array(
				'products' => $products,
				'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),
				'homeSize' => Image::getSize(ImageType::getFormatedName('home')),
			));
		return $this->display(__FILE__, 'homefeatured.tpl');
	}

Sorry my bad english.

 

Thank you very much.

This is the correct way for the ps1.5.6.2

  • Like 1

Share this post


Link to post
Share on other sites

Thank you very much.

This is the correct way for the ps1.5.6.2

Thanks, It works in 1.5.6.2

But only random the first 8 articles...

How can I tell the module to take all items in home to show them randomly?

Share this post


Link to post
Share on other sites

Thanks, It works in 1.5.6.2

But only random the first 8 articles...

How can I tell the module to take all items in home to show them randomly?

 

change line:

$products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), "position");
 

to:

$products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), "position",null,null,true,true);
  • Like 1

Share this post


Link to post
Share on other sites

 

change line:

$products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), "position");
 

to:

$products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), "position",null,null,true,true);

Hi. Thanks for the help.

But it doesn´t work.

it takes all the articles in home and random them but only show 1 in main page.

Share this post


Link to post
Share on other sites

Hi. Thanks for the help.

But it doesn´t work.

it takes all the articles in home and random them but only show 1 in main page.

 

I have the same problem as designerspain. Although its on 1.6 I presume that I can use the same solution as he would get :) Am following

Share this post


Link to post
Share on other sites

I'm having the same problem, after making changes products are random, but only one.

Share this post


Link to post
Share on other sites

I think no one knows how to solve this.

Is there any way to show the new items in home module?

 

I think we all want to not be shown always the same products. So if we can´t random them if it is possible to make the module show the new ones can be a good solution too.

Share this post


Link to post
Share on other sites

it's neessary to remove caching from this module (all caching functions) and of course to change this:

$category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), 'position');

to function already mentioned somewhere above

  • Like 1

Share this post


Link to post
Share on other sites

this worked for me 
 
 
$products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 10), NULL,NULL,false,true,true,($nb ? $nb : 50));

 

on 1.5.6.2

  • Like 1

Share this post


Link to post
Share on other sites

any clues for 1.6 ?

 

for :

 

NEW ARRIVALS

 

POPULAR PRODUCTS

 

BEST SELLERS

Share this post


Link to post
Share on other sites

worked for me with this code:
 

public function hookDisplayHome($params)
{
$category = new Category(Context::getContext()->shop->getCategory(), Configuration::get('PS_LANG_DEFAULT'));
$shuffle = (Configuration::get('HOME_FEATURED_RNBR')) ? true:false;
$rnb = ($shuffle) ? (int)Configuration::get('HOME_FEATURED_RNBR'):(int)Configuration::get('HOME_FEATURED_NBR');
$nb = (int)(Configuration::get('HOME_FEATURED_NBR'));
$products = $category->getProducts((int)Context::getContext()->language->id, 1, ($rnb ? $rnb : 10), "position",NULL,NULL,true,true,($nb ? $nb : 50));
 
if ($products) {
       shuffle($products);
       array_splice($products, ($nb ? $nb : 10));
}
 
$this->smarty->assign(array(
'products' => $products,
'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),
'homeSize' => Image::getSize('home'),
));
 
 
 
ty FOR ALL POSTS

Share this post


Link to post
Share on other sites

 

worked for me with this code:

 

public function hookDisplayHome($params)
{
$category = new Category(Context::getContext()->shop->getCategory(), Configuration::get('PS_LANG_DEFAULT'));
$shuffle = (Configuration::get('HOME_FEATURED_RNBR')) ? true:false;
$rnb = ($shuffle) ? (int)Configuration::get('HOME_FEATURED_RNBR'):(int)Configuration::get('HOME_FEATURED_NBR');
$nb = (int)(Configuration::get('HOME_FEATURED_NBR'));
$products = $category->getProducts((int)Context::getContext()->language->id, 1, ($rnb ? $rnb : 10), "position",NULL,NULL,true,true,($nb ? $nb : 50));
 
if ($products) {
       shuffle($products);
       array_splice($products, ($nb ? $nb : 10));
}
 
$this->smarty->assign(array(
'products' => $products,
'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),
'homeSize' => Image::getSize('home'),
));
 
 
 
ty FOR ALL POSTS

 

Work in Ps 1.6 ?

Share this post


Link to post
Share on other sites

it's neessary to remove caching from this module (all caching functions) and of course to change this:

$category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), 'position');

to function already mentioned somewhere above

Hi Vekia! I try to random product display in my shop, presta 1.5.6.1, i have try your code 

http://mypresta.eu/en/art/tips-and-tricks/how-to-display-products-randomly-in-prestashop-home-featured-module.html

but don't work. Do you can help me? i don't found the error, my homefeatured.php:

if (!defined('_PS_VERSION_'))
	exit;

class HomeFeatured extends Module
{
	private $_html = '';
	private $_postErrors = array();

	function __construct()
	{
		$this->name = 'homefeatured';
		$this->tab = 'front_office_features';
		$this->version = '1.1';
		$this->author = 'PrestaShop';
		$this->need_instance = 0;

		parent::__construct();

		$this->displayName = $this->l('Featured products on the homepage.');
		$this->description = $this->l('Displays featured products in the middle of your homepage.');
	}

	function install()
	{
		$this->_clearCache('homefeatured.tpl');
		Configuration::updateValue('HOME_FEATURED_NBR', 8);

		if (!parent::install()
			|| !$this->registerHook('displayHome')
			|| !$this->registerHook('header')
			|| !$this->registerHook('addproduct')
			|| !$this->registerHook('updateproduct')
			|| !$this->registerHook('deleteproduct')
		)
			return false;
		return true;
	}
	
	public function uninstall()
	{
		$this->_clearCache('homefeatured.tpl');
		return parent::uninstall();
	}

	public function getContent()
	{
		$output = '<h2>'.$this->displayName.'</h2>';
		if (Tools::isSubmit('submitHomeFeatured'))
		{
			$nbr = (int)Tools::getValue('nbr');
			if (!$nbr OR $nbr <= 0 OR !Validate::isInt($nbr))
				$errors[] = $this->l('An invalid number of products has been specified.');
			else
				Configuration::updateValue('HOME_FEATURED_NBR', (int)($nbr));
			if (isset($errors) AND sizeof($errors))
				$output .= $this->displayError(implode('<br />', $errors));
			else
				$output .= $this->displayConfirmation($this->l('Your settings have been updated.'));
		}
		return $output.$this->displayForm();
	}

	public function displayForm()
	{
		$output = '
		<form action="'.Tools::safeOutput($_SERVER['REQUEST_URI']).'" method="post">
			<fieldset><legend><img src="'.$this->_path.'logo.gif" alt="" title="" />'.$this->l('Settings').'</legend>
				<p>'.$this->l('To add products to your homepage, simply add them to the "home" category.').'</p><br />
				<label>'.$this->l('Define the number of products to be displayed.').'</label>
				<div class="margin-form">
					<input type="text" size="5" name="nbr" value="'.Tools::safeOutput(Tools::getValue('nbr', (int)(Configuration::get('HOME_FEATURED_NBR')))).'" />
					<p class="clear">'.$this->l('Define the number of products that you would like to display on homepage (default: 8).').'</p>

				</div>
				<center><input type="submit" name="submitHomeFeatured" value="'.$this->l('Save').'" class="button" /></center>
			</fieldset>
		</form>';
		return $output;
	}

	public function hookDisplayHeader($params)
	{
		$this->hookHeader($params);
	}

	public function hookHeader($params)
	{
		$this->context->controller->addCSS(($this->_path).'homefeatured.css', 'all');
	}

public function hookDisplayHome($params){
	$category = new Category(Context::getContext()->shop->getCategory(), (int)Context::getContext()->language->id);
	$nb = (int)(Configuration::get('HOME_FEATURED_NBR'));
	$products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 10));
 
	shuffle($products);
 
	$this->smarty->assign(array(
		'products' => $products,
		'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),
		'homeSize' => Image::getSize('home_default'),
	));
	return $this->display(__FILE__, 'homefeatured.tpl');
}

	public function hookAddProduct($params)
	{
		$this->_clearCache('homefeatured.tpl');
	}

	public function hookUpdateProduct($params)
	{
		$this->_clearCache('homefeatured.tpl');
	}

	public function hookDeleteProduct($params)
	{
		$this->_clearCache('homefeatured.tpl');
	}
}

Share this post


Link to post
Share on other sites