Jump to content

Random product display on featured page


Recommended Posts

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
Link to comment
Share on other sites

  • 3 months later...

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
Link to comment
Share on other sites

  • 2 months later...

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)
Link to comment
Share on other sites

  • 2 weeks later...
  • 3 weeks later...

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
Link to comment
Share on other sites

  • 3 weeks later...

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

Link to comment
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)
Link to comment
Share on other sites

  • 1 month later...

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

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

Link to comment
Share on other sites

  • 3 weeks later...

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));

}

Link to comment
Share on other sites

  • 2 weeks later...

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.

Link to comment
Share on other sites

  • 2 weeks later...

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 !
Link to comment
Share on other sites

  • 2 weeks later...

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...

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

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

Link to comment
Share on other sites

  • 2 weeks later...

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');
}
}

Link to comment
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
Link to comment
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
Link to comment
Share on other sites

  • 2 weeks later...

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)
Link to comment
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
Link to comment
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
Link to comment
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
Link to comment
Share on other sites

  • 2 months later...
  • 3 weeks later...

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!!!
Link to comment
Share on other sites

  • 4 weeks later...

$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

Link to comment
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!!!

Link to comment
Share on other sites

  • 1 month later...
  • 3 weeks later...
  • 2 weeks later...

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)

Link to comment
Share on other sites

  • 1 month later...

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)
Link to comment
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)
Link to comment
Share on other sites

  • 3 weeks later...

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)
Link to comment
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
Link to comment
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.

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

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

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

Link to comment
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
Link to comment
Share on other sites

  • 3 weeks later...

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

Link to comment
Share on other sites

  • 1 month later...

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');
	}
}

Link to comment
Share on other sites

Finally I read one and other time the whole topic... Im lost :(  Im using 1.6.0.5..  I need to random products from all my categories in homefeatured module...  some help pls..

do you used some solutions provided here? or just read only?

Link to comment
Share on other sites

do you used some solutions provided here? or just read only?

I ever read almost all... then when I open the homefeatured.php I didnt find where to place or replace any code.. I mind is becasuse Im with 1.6.0.5 ...?  the code I open do not have any similar to the examples shown here... I try some but nothing happend..

Link to comment
Share on other sites

public function hookDisplayHome($params)
	{
		if (!$this->isCached('homefeatured.tpl', $this->getCacheId()))
		{
			$this->_cacheProducts();
			$this->smarty->assign(
				array(
					'products' => HomeFeatured::$cache_products,
					'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),
					'homeSize' => Image::getSize(ImageType::getFormatedName('home')),
				)
			);
		}

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

change it to:

public function hookDisplayHome($params)
	{
		if (!$this->isCached('homefeatured.tpl', $this->getCacheId()))
		{
			$this->_cacheProducts();

$category = new Category(Context::getContext()->shop->getCategory(), (int)Context::getContext()->language->id);
$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(
					'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());
	}
  • Like 2
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...