Jump to content

Random home featured in v1.6


St055

Recommended Posts

on homefeutured.php about row 113 you will find something like this

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

Coment this text and paste this

 

HomeFeatured::$cache_products = $category->getProducts((int)Context::getContext()->language->id, 1, 100, 'position');
      if (HomeFeatured::$cache_products) {
        shuffle(HomeFeatured::$cache_products);
        array_splice(HomeFeatured::$cache_products, ($nb ? $nb : 8));
  • Like 2
Link to comment
Share on other sites

Hi 

 

Change that:

 

domain/module/homefeatured/homefeatured.php

 

 

-----------------------------------------

 

 

OLD:

 

 

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());
}
 
 
 
 
 
 
----------------------------------------------------------------------------------------------
 
NEW:
 
 
 
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));*/
$products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 10), 'date_add', 'DESC', false, true, true, $nb);

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

--------------------------------------------

 

 

:)

Edited by Daniel - PrestaBR (see edit history)
  • Like 8
Link to comment
Share on other sites

  • 5 weeks later...

 

use this:

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

Thanks Vekia, I will try this as soon as I am on my PC. Should I edit stock homefeatured.php or this edited one?

 

Regards,

 

Vedran

Link to comment
Share on other sites

Hi Vekia, 

Thanks for you reply.

I just replaced the following line in my edited homefeatured.php

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

With yours

 

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

 

But then I get following error:

Notice: Undefined variable: category in /home5/hejkupim/public_html/test/modules/homefeatured/homefeatured.php on line 129

Fatal error: Call to a member function getProducts() on a non-object in /home5/hejkupim/public_html/test/modules/homefeatured/homefeatured.php on line 129
 
This is my edited code:
public function hookDisplayHome($params){
$category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), 'position',null,false,true,true);$nb = (int)(Configuration::get('HOME_FEATURED_NBR'));
/*$products = $category->getProducts((int)Context::getContext()->language->id, 7, ($nb ? $nb : 10));*/
$products = $category->getProducts((int)Context::getContext()->language->id, 7, ($nb ? $nb : 10), 'date_add', 'DESC', false, true, true, $nb);


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');
}
What am I doing wrong, did I edited the right line?
 
 
Thanks in advance.

Regards,

Vedran
Link to comment
Share on other sites

 

you have to use code that i suggested instead of:

$products = $category->getProducts((int)Context::getContext()->language->id, 7, ($nb ? $nb : 10), 'date_add', 'DESC', false, true, true, $nb);

Hi Vekia,

 

I just did that, but same 10-15 products are randomly displayed when refreshed (I have more than 15 products in my home category). I Also have this modification on my live 1.5 site which is working great.

 

Edit:

 

Actually, I got an following error:

 

Warning: shuffle() expects parameter 1 to be array, null given in /home5/hejkupim/public_html/test/modules/homefeatured/homefeatured.php on line 134

And no homefeaured products, just this message: No featured products at this time.

 

Here is my code

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));*/
$category->getProducts((int)Context::getcontext()->language->id, 1, ($nb ? $nb : 8), 'position',null,false,true,true);


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

My test site: http://hejkupi.me/test

Regards,

 

Vedran

Edited by vmarkovic (see edit history)
Link to comment
Share on other sites

  • 2 weeks later...
$category->getProducts((int)Context::getcontext()->language->id, 1, ($nb ? $nb : 8), 'position',null,false,true,true);

to

$products=$category->getProducts((int)Context::getcontext()->language->id, 1, ($nb ? $nb : 8), 'position',null,false,true,true);
Link to comment
Share on other sites

sorry for misunderstanding but my post was to vmarkovic.

in your case it's necessary to modify core function getProducts, but in this case it will affect also other part of shop where the product listings are visible

Link to comment
Share on other sites

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

I think a " } " was missed and that's why is not working...i put at the end of the line the  } and works for my 

HomeFeatured::$cache_products = $category->getProducts((int)Context::getContext()->language->id, 1, 100, 'position');
 if (HomeFeatured::$cache_products) {
	shuffle(HomeFeatured::$cache_products);
	array_splice(HomeFeatured::$cache_products, ($nb ? $nb : 8));
}

Look the code, there is an if.

Then Clean the Cache from the store, and it's work!,

Link to comment
Share on other sites

  • 2 weeks later...

The changes only randomise the same 20 products for my store, i want to be able to get a total random selection of products not just move around the same products. 

 

www.w2g.co.uk 

 

thanks in advance for any help

 

lots of posts are confusing does anyone have simpled down version for a total random selection of products from you entire inventory? 

Edited by mike117 (see edit history)
Link to comment
Share on other sites

  • 3 weeks later...

Just change this function:

 

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

 

for this code:

 

 

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));*/
$products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 10), 'date_add', 'DESC', false, true, true, $nb);
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');
}

 

 

Replace the whole function, and it works.

Link to comment
Share on other sites

  • 1 month later...

Hi webonmaster,

You have to edit /public_html/modules/homefeatured/homefeatured.php.

Replace this part:

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

with 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 : 10));*/
$products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 10), 'date_add', 'DESC', false, true, true, $nb);

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

I just tried this on my 1.6.0.9 - it works.

Regards, 

Vedran

  • Like 1
Link to comment
Share on other sites

Hi Verdan,

I try your edit but it works only if I switch between New arrivals and Popular. But I have Popular on 1st switch and New arrivals on 2nd switch on my homepage. When I come to my website I can see on both switches (Popular and New arrivals) the same products. Your edit take effect after I switch Popular -> News and back to Popular and take effect only once. I have to refresh my website, switch Popular -> New arrivals and back and after this product random. Do I something wrong?

Thanks for help.

jirikcz

 

Hi webonmaster,

You have to edit /public_html/modules/homefeatured/homefeatured.php.

Replace this part:

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

with 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 : 10));*/
$products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 10), 'date_add', 'DESC', false, true, true, $nb);

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

I just tried this on my 1.6.0.9 - it works.

Regards, 

Vedran

Edited by jirikcz (see edit history)
Link to comment
Share on other sites

Hi Verdan,

I try your edit but it works only if I switch between New arrivals and Popular. But I have Popular on 1st switch and New arrivals on 2nd switch on my homepage. When I come to my website I can see on both switches (Popular and New arrivals) the same products. Your edit take effect after I switch Popular -> News and back to Popular and take effect only once. I have to refresh my website, switch Popular -> New arrivals and back and after this product random. You can see it on my website at http://76345.w45.wedos.ws/en/ . Do I something wrong?

Thanks for help.

jirikcz

Hi jirikcz,

 

I just copy/pasted code and instructions from this topic so I am not sure are you doing something wrong.

 

Regards,

 

Vedrav

Link to comment
Share on other sites

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

Hi DARF3D3

 

You have to change ".../modules/homefeatured/homefeatured.php"

 

at line 107

replace this:

	public function _cacheProducts()
	{
		if (!isset(HomeFeatured::$cache_products))
		{
			$category = new Category(Context::getContext()->shop->getCategory(), (int)Context::getContext()->language->id);
			$nb = (int)Configuration::get('HOME_FEATURED_NBR');
			HomeFeatured::$cache_products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), 'position');
		}

		if (HomeFeatured::$cache_products === false || empty(HomeFeatured::$cache_products))
			return false;
	}

with this:

	public function _cacheProducts()
	{
		if (!isset(HomeFeatured::$cache_products))
		{
			$category = new Category(Context::getContext()->shop->getCategory(), (int)Context::getContext()->language->id);
			$nb = (int)Configuration::get('HOME_FEATURED_NBR');
			/*HomeFeatured::$cache_products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), 'position');*/
			HomeFeatured::$cache_products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 10), NULL,NULL,false,true,true,($nb ? $nb : 100));
		}

		if (HomeFeatured::$cache_products === false || empty(HomeFeatured::$cache_products))
			return false;
	}

Regards,

 

prestatuga

 

  • Like 1
Link to comment
Share on other sites

Hi DARF3D3

 

Sorry for my mistake and the incomplete information.
In my shop I added all products to the home category besides having its own main category.
In this store have less than 100 items. If necessary, you can increase the number of random items.
This is not the most elegant solution, but it was the only one that got it working.
I do not have many products and in my case it was easy. In a very large store will be painful to edit all items.

 

best regards,

Prestatuga

Link to comment
Share on other sites

can someone please update the new hole homefeatured.php file, for just replace it

 

thanks


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

class HomeFeatured extends Module
{
	protected static $cache_products;

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

		$this->bootstrap = true;
		parent::__construct();

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

	public function install()
	{
		$this->_clearCache('*');
		Configuration::updateValue('HOME_FEATURED_NBR', 8);
		Configuration::updateValue('HOME_FEATURED_CAT', (int)Context::getContext()->shop->getCategory());
		Configuration::updateValue('HOME_FEATURED_RANDOMIZE', false);

		if (!parent::install()
			|| !$this->registerHook('header')
			|| !$this->registerHook('addproduct')
			|| !$this->registerHook('updateproduct')
			|| !$this->registerHook('deleteproduct')
			|| !$this->registerHook('categoryUpdate')
			|| !$this->registerHook('displayHomeTab')
			|| !$this->registerHook('displayHomeTabContent')
		)
			return false;

		return true;
	}

	public function uninstall()
	{
		$this->_clearCache('*');

		return parent::uninstall();
	}

	public function getContent()
	{
		$output = '';
		$errors = array();
		if (Tools::isSubmit('submitHomeFeatured'))
		{
			$nbr = Tools::getValue('HOME_FEATURED_NBR');
			if (!Validate::isInt($nbr) || $nbr <= 0)
			$errors[] = $this->l('The number of products is invalid. Please enter a positive number.');

			$cat = Tools::getValue('HOME_FEATURED_CAT');
			if (!Validate::isInt($cat) || $cat <= 0)
				$errors[] = $this->l('The category ID is invalid. Please choose an existing category ID.');
				
			$rand = Tools::getValue('HOME_FEATURED_RANDOMIZE');
			if (!Validate::isBool($rand))
				$errors[] = $this->l('Invalid value for the "randomize" flag.');
			if (isset($errors) && count($errors))
				$output = $this->displayError(implode('<br />', $errors));
			else
			{
				Configuration::updateValue('HOME_FEATURED_NBR', (int)$nbr);
				Configuration::updateValue('HOME_FEATURED_CAT', (int)$cat);
				Configuration::updateValue('HOME_FEATURED_RANDOMIZE', (bool)$rand);
				Tools::clearCache(Context::getContext()->smarty, $this->getTemplatePath('homefeatured.tpl'));
				$output = $this->displayConfirmation($this->l('Your settings have been updated.'));
			}
		}

		return $output.$this->renderForm();
	}

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

	public function hookHeader($params)
	{
		if (isset($this->context->controller->php_self) && $this->context->controller->php_self == 'index')
			$this->context->controller->addCSS(_THEME_CSS_DIR_.'product_list.css');
		$this->context->controller->addCSS(($this->_path).'homefeatured.css', 'all');
	}

	public function _cacheProducts()
	{
		if (!isset(HomeFeatured::$cache_products))
		{
			$category = new Category((int)Configuration::get('HOME_FEATURED_CAT'), (int)Context::getContext()->language->id);
			$nb = (int)Configuration::get('HOME_FEATURED_NBR');
			if (Configuration::get('HOME_FEATURED_RANDOMIZE'))
				HomeFeatured::$cache_products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), null, null, false, true, true, ($nb ? $nb : 8));
			else
				HomeFeatured::$cache_products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), 'position');
		}

		if (HomeFeatured::$cache_products === false || empty(HomeFeatured::$cache_products))
			return false;
	}

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

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

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

	public function hookDisplayHomeTabContent($params)
	{
		return $this->hookDisplayHome($params);
	}

	public function hookAddProduct($params)
	{
		$this->_clearCache('*');
	}

	public function hookUpdateProduct($params)
	{
		$this->_clearCache('*');
	}

	public function hookDeleteProduct($params)
	{
		$this->_clearCache('*');
	}

	public function hookCategoryUpdate($params)
	{
		$this->_clearCache('*');
	}

	public function _clearCache($template, $cache_id = NULL, $compile_id = NULL)
	{
		parent::_clearCache('homefeatured.tpl');
		parent::_clearCache('tab.tpl', 'homefeatured-tab');
	}

	public function renderForm()
	{
		$fields_form = array(
			'form' => array(
				'legend' => array(
					'title' => $this->l('Settings'),
					'icon' => 'icon-cogs'
				),
				'description' => $this->l('To add products to your homepage, simply add them to the corresponding product category (default: "Home").'),
				'input' => array(
					array(
						'type' => 'text',
						'label' => $this->l('Number of products to be displayed'),
						'name' => 'HOME_FEATURED_NBR',
						'class' => 'fixed-width-xs',
						'desc' => $this->l('Set the number of products that you would like to display on homepage (default: 8).'),
					),
					array(
						'type' => 'text',
						'label' => $this->l('Category from which to pick products to be displayed'),
						'name' => 'HOME_FEATURED_CAT',
						'class' => 'fixed-width-xs',
						'desc' => $this->l('Choose the category ID of the products that you would like to display on homepage (default: 2 for "Home").'),
					),
					array( 
						'type' => 'switch',
						'label' => $this->l('Randomly display featured products'),
						'name' => 'HOME_FEATURED_RANDOMIZE',
						'class' => 'fixed-width-xs',
						'desc' => $this->l('Enable if you wish the products to be displayed randomly (default: no).'),
						'values' => array(
							array(
								'id' => 'active_on',
								'value' => 1,
								'label' => $this->l('Yes')
							),
							array(
								'id' => 'active_off',
								'value' => 0,
								'label' => $this->l('No')
							)
						),
					),
				),
				'submit' => array(
					'title' => $this->l('Save'),
				)
			),
		);

		$helper = new HelperForm();
		$helper->show_toolbar = false;
		$helper->table = $this->table;
		$lang = new Language((int)Configuration::get('PS_LANG_DEFAULT'));
		$helper->default_form_language = $lang->id;
		$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0;
		$this->fields_form = array();
		$helper->id = (int)Tools::getValue('id_carrier');
		$helper->identifier = $this->identifier;
		$helper->submit_action = 'submitHomeFeatured';
		$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false).'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
		$helper->token = Tools::getAdminTokenLite('AdminModules');
		$helper->tpl_vars = array(
			'fields_value' => $this->getConfigFieldsValues(),
			'languages' => $this->context->controller->getLanguages(),
			'id_language' => $this->context->language->id
		);

		return $helper->generateForm(array($fields_form));
	}

	public function getConfigFieldsValues()
	{
		return array(
			'HOME_FEATURED_NBR' => Tools::getValue('HOME_FEATURED_NBR', (int)Configuration::get('HOME_FEATURED_NBR')),
			'HOME_FEATURED_CAT' => Tools::getValue('HOME_FEATURED_CAT', (int)Configuration::get('HOME_FEATURED_CAT')),
			'HOME_FEATURED_RANDOMIZE' => Tools::getValue('HOME_FEATURED_RANDOMIZE', (bool)Configuration::get('HOME_FEATURED_RANDOMIZE')),
		);
	}
}

  • Like 1
Link to comment
Share on other sites

  • 1 month later...
  • 2 months later...

Why was the method originally date_add then changed to position?

I am using date_add but after I saw this post using position, I'm trying to understand getProducts different orders.

 

 if ($order_by == 'id_product' || $order_by == 'price' || $order_by == 'date_add' || $order_by == 'date_upd')
                        $order_by_prefix = 'p';
                else if ($order_by == 'name')
                        $order_by_prefix = 'pl';
                else if ($order_by == 'position')
                        $order_by_prefix = 'c';
 
p, pl, and c. 
Can someone explain please?
 
TIA
Edited by zeki893 (see edit history)
Link to comment
Share on other sites

×
×
  • Create New...