Jump to content

How to display random products?


Sharak

Recommended Posts

I know how to randomize featured products. I need to display on my homepage 3 random products from whole offer, not just from featured. Default featured products are also on my homepage so it has to be new functionality of homefeatured. So far I did that in homefeatured.php:

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);
			$oferta = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 100));
			if ($oferta) {
				shuffle($oferta);
				array_splice($oferta, ($nb ? $nb : 3));
			}

			$this->smarty->assign(
				array(
					'oferta' => $oferta,
					'ofertaSize' => Image::getSize(ImageType::getFormatedName('oferta')),
					'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());
	}

Added lines 134-139, 143-144

 

Also added this in homefeatured.tpl:

<div id="oferta" class="block products_block clearfix">
	<h4 class="title_block">{l s='Offer' mod='homefeatured'}</h4>
	<div class="block_content">
		<ul>
			{foreach from=$oferta item=product name=ofertaProducts}
				<li class="ajax_block_product {if $smarty.foreach.ofertaProducts.first}first_item{elseif $smarty.foreach.ofertaProducts.last}last_item{else}item{/if}">
					<a href="{$product.link|escape:'html'}" title="{$product.name|escape:html:'UTF-8'}" class="product_image"><img src="{$link->getImageLink($product.link_rewrite, $product.id_image, 'oferta_default')|escape:'html'}" height="{$ofertaSize.height}" width="{$ofertaSize.width}" alt="{$product.name|escape:html:'UTF-8'}" />{if isset($product.new) && $product.new == 1}<span class="new">{l s='New' mod='homefeatured'}</span>{/if}<h5 class="s_title_block">{$product.name|truncate:35:'...'|escape:'htmlall':'UTF-8'}</h5></a>
					<div class="product_desc"><a href="{$product.link|escape:'html'}" title="{l s='More' mod='homefeatured'}">{$product.description_short|strip_tags|truncate:80:'...'}</a></div>
					<div>
						{if $product.show_price AND !isset($restricted_country_mode) AND !$PS_CATALOG_MODE}<p class="price_container">{l s='Price' mod='homefeatured'}:<br/><span class="price">{if !$priceDisplay}{convertPrice price=$product.price}{else}{convertPrice price=$product.price_tax_exc}{/if}</span></p>{else}<div style="height:21px;"></div>{/if}
						<a class="lnk_more" href="{$product.link|escape:'html'}" title="{l s='View' mod='homefeatured'}"></a>
					</div>
				</li>
			{/foreach}
			</ul>
	</div>
</div>

Products are loaded just fine (3 random products). Thing is the Offer is still generated from featured products, instead whole offer. I guess this 2 lines have to be changed to fix this:

$category = new Category(Context::getContext()->shop->getCategory(), (int)Context::getContext()->language->id);
$oferta = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 100));
  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

I think we have to try some admin functions for that as there isn't any for loading all products in front end. AdminProductsController loads list of all products. We just need to separate products according to display configuration (throw out disabled, out of stock - if shop doesn't allow buying , etc), randomize it and generate small list out of it. Question is: how to do it? :)

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

in classes/Product.php you have the following function

 


public static function getSimpleProducts($id_lang, Context $context = null)

 


Which may come in useful here:

 

 

So in you hookDisplayHome (or any other function you create in your own module to get the products)

 

change something like:

 


  1. $category = new Category(Context::getContext()->shop->getCategory(), (int)Context::getContext()->language->id); // remove this line
  2. $oferta = Product::getSimpleProducts((int)Context::getContext()->language->id);
  3. if ($oferta) {
  4. shuffle($oferta);
  5. array_splice($oferta, ($nb ? $nb : 3));
  6. }

 


After that you might need to get the full product objects, so walk through the $oferta array and get their object from the id_product you have in the array.


 



Hope this helps,

pascal

Link to comment
Share on other sites

There are sooooo many answers and ways to do just one thing "Randomizing Product display" ... this is all a little "ALOT" confusing. I cal link up different threads from people telling you what and how to randomly display products on the site, one person says do this, the other says no, do it this way, and yet another says .. no, my way is the right way .. argghh!

Link to comment
Share on other sites

I guess you have to try all the possibilities and go with the best that suits your needs :)

 

@PascalVG

Thanks. This way works well as to pick from whole offer, but it uses only id and product name. So I guess I'll have to mix and match my own function. I definitely could use your help on that ;)

Link to comment
Share on other sites

Just use the id_product's you selected to get the full object, like

 

$myProduct = new Product(<one_of_the_3_ids>, true, $context->language->id);

 

This should give you the other fields for the product you need, like price, manufacturer, tags, stock, category etc.

 

pascal

Link to comment
Share on other sites

  • 1 month later...
  • 1 month later...

I found many solutions for showing featured products randomly, but how to display random products from whole offer instead just from homefeatured? To be specific, I need 3 random products.

I would like to know how to do this too, very usefull thing for shop, I think.

Link to comment
Share on other sites

  • 2 months later...

Just use the id_product's you selected to get the full object, like

 

$myProduct = new Product(<one_of_the_3_ids>, true, $context->language->id);

 

This should give you the other fields for the product you need, like price, manufacturer, tags, stock, category etc.

 

pascal

How am I suppose to use above in my code? Tried everything I know of - doesn't work :(

Only parameters I get is ID, product name and image type size. It doesn't load description, images, price etc. not even a link to the product.

 

My current code in homefeatured.php function hookDisplayHome($params):

$oferta = Product::getSimpleProducts((int)Context::getContext()->language->id);
	if ($oferta) {
		shuffle($oferta);
		array_splice($oferta, 3);
	}

$this->smarty->assign(
	array(
		'oferta' => $oferta,
		'ofertaSize' => Image::getSize(ImageType::getFormatedName('oferta')),
		'products' => HomeFeatured::$cache_products,
		'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),
		'homeSize' => Image::getSize(ImageType::getFormatedName('home')),
	)
);

and part of homefeatured.tpl:

<div id="oferta" class="block products_block clearfix">
	<h4 class="title_block">{l s='Offer' mod='homefeatured'}</h4>
	<div class="block_content">
		<ul>
			{foreach from=$oferta item=product name=ofertaProducts}
				<li class="ajax_block_product {if $smarty.foreach.ofertaProducts.first}first_item{elseif $smarty.foreach.ofertaProducts.last}last_item{else}item{/if}">
					<a href="{$product.link|escape:'html'}" title="{$product.name|escape:html:'UTF-8'}" class="product_image"><img src="{$link->getImageLink($product.link_rewrite, $product.id_image, 'oferta_default')|escape:'html'}" height="{$ofertaSize.height}" width="{$ofertaSize.width}" alt="{$product.name|escape:html:'UTF-8'}" />
					{if isset($product.new) && $product.new == 1}<span class="new">{l s='New' mod='homefeatured'}</span>{/if}<h5 class="s_title_block">{$product.name|truncate:35:'...'|escape:'htmlall':'UTF-8'}</h5></a>
					<div class="product_desc"><a href="{$product.link|escape:'html'}" title="{l s='More' mod='homefeatured'}">{$product.description_short|strip_tags|truncate:80:'...'}</a></div>
					<div>
						{if $product.show_price AND !isset($restricted_country_mode) AND !$PS_CATALOG_MODE}<p class="price_container">{l s='Price' mod='homefeatured'}:<br/><span class="price">{if !$priceDisplay}{convertPrice price=$product.price}{else}{convertPrice price=$product.price_tax_exc}{/if}</span></p>{else}<div style="height:21px;"></div>{/if}
						<a class="lnk_more" href="{$product.link|escape:'html'}" title="{l s='View' mod='homefeatured'}"></a>
					</div>
				</li>
			{/foreach}
			</ul>
	</div>
</div>
Edited by Sharak (see edit history)
Link to comment
Share on other sites

  • razaro locked this topic
Guest
This topic is now closed to further replies.
×
×
  • Create New...