Sharak Posted June 10, 2014 Share Posted June 10, 2014 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. 1 Link to comment Share on other sites More sharing options...
Sharak Posted June 13, 2014 Author Share Posted June 13, 2014 Anyone? Link to comment Share on other sites More sharing options...
vekia Posted June 13, 2014 Share Posted June 13, 2014 you're talking about homefeatured? or about each feature (which displays products) available in your shoP? Link to comment Share on other sites More sharing options...
Sharak Posted June 13, 2014 Author Share Posted June 13, 2014 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)); 1 Link to comment Share on other sites More sharing options...
Sharak Posted June 23, 2014 Author Share Posted June 23, 2014 (edited) 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 June 23, 2014 by Sharak (see edit history) Link to comment Share on other sites More sharing options...
PascalVG Posted June 23, 2014 Share Posted June 23, 2014 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: $category = new Category(Context::getContext()->shop->getCategory(), (int)Context::getContext()->language->id); // remove this line $oferta = Product::getSimpleProducts((int)Context::getContext()->language->id); if ($oferta) { shuffle($oferta); array_splice($oferta, ($nb ? $nb : 3)); } 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 More sharing options...
LineTV Posted June 23, 2014 Share Posted June 23, 2014 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 More sharing options...
Sharak Posted June 24, 2014 Author Share Posted June 24, 2014 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 More sharing options...
PascalVG Posted June 25, 2014 Share Posted June 25, 2014 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 More sharing options...
mcbain.neil Posted August 6, 2014 Share Posted August 6, 2014 Sharak, Did you come right with this. Becoming a bit of a tiring excercise to try and get this right? Link to comment Share on other sites More sharing options...
febeks Posted September 11, 2014 Share Posted September 11, 2014 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 More sharing options...
Sharak Posted December 4, 2014 Author Share Posted December 4, 2014 (edited) 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 December 4, 2014 by Sharak (see edit history) Link to comment Share on other sites More sharing options...
Recommended Posts