uddhava Posted February 18, 2010 Share Posted February 18, 2010 How do i get a random product image as the subcategory when there was no subcategory image used?I was looking around in the category.tpl file. It has to do with this code: {if $subcategory.id_image} getCatImageLink($subcategory.link_rewrite, $subcategory.id_image, 'medium')}" alt="" /> {else} {/if} How to get a random product pic, or even only the first product pic in this category?I also looked in the product.tpl and found this code: img src="{$link->getImageLink($product.link_rewrite, $product.id_image, 'home')}" How to get the 2 mixed so we see a product image instead of the category image when none is defined? Link to comment Share on other sites More sharing options...
rocky Posted February 19, 2010 Share Posted February 19, 2010 You could get the first product image when no category image is available by changing lines 15-17 of category.tpl in your theme's directory from: <!-- Category image --> {if $category->id_image} <img src="{$link->getCatImageLink($category->link_rewrite, $category->id_image, 'category')}" alt="{$category->name|escape:'htmlall':'UTF-8'}" title="{$category->name|escape:'htmlall':'UTF-8'}" id="categoryImage" /> {/if} to: <!-- Category image --> {if $category->id_image} <img src="{$link->getCatImageLink($category->link_rewrite, $category->id_image, 'category')}" alt="{$category->name|escape:'htmlall':'UTF-8'}" title="{$category->name|escape:'htmlall':'UTF-8'}" id="categoryImage" /> {else} <img src="{$link->getImageLink($products.0.link_rewrite, $products.0.id_image, 'category')}" alt="{$products.0.name|escape:'htmlall':'UTF-8'}" title="{$products.0.name|escape:'htmlall':'UTF-8'}" id="categoryImage" /> {/if} It is possible to get a random image too, but the code is more complicated. Link to comment Share on other sites More sharing options...
uddhava Posted February 20, 2010 Author Share Posted February 20, 2010 Thanks rocky, but this does not work.So i have fixed that and tried your solution, but it does not seem to work. I get the familiar ? pic.But first i like to say that you did not understand my request, or i was not clear.You proposed some code to change the category pic. My request was for the subcategories. Sorry to be unclear.So here is the code that i tried now : {if $subcategory.id_image} getCatImageLink($subcategory.link_rewrite, $subcategory.id_image, 'medium')}" alt="" /> {else} getImageLink($products.0.link_rewrite, $products.0.id_image, 'category')}" alt="{$products.0.name|escape:'htmlall':'UTF-8'}" title="{$products.0.name|escape:'htmlall':'UTF-8'}" id="categoryImage" /> {/if} But i tried to put a harcoded image in the ELSE clausule, but that will not work.For some reason the IF Statement is always true. And then the default "No image available" image is used.So it will never go to the ELSE statement.I have 1 product in one of the subcategories.For example my category structure (with id) is now :Dames (13 > Body Art (14)The product ID is 24. And the image are called (in /img/p)24-74-medium.jpg24-74-large.jpg etcThe link that i get in the website source page is this : So whats happening here? Link to comment Share on other sites More sharing options...
uddhava Posted February 20, 2010 Author Share Posted February 20, 2010 If i could make another suggestion :The code checks if there is a category image available and then sets the correct image.I would like to add the following :1) first check if there is an subcategory image {if $subcategory.id_image}2) then check if this subcategory has at least 1 product image3) if YES then set the image from the product4) if NO then set the image from the subcat5) if no image at all then set the default "no image available"I checked the product.tpl for the needed code, but it is difficult to find it.Here is my suggested code (does not work yet) {if $subcategory.id_image} {IF PRODUCT IMAGE IS AVAILABLE} {SET PRODUCT IMAGE AS SUBCAT IMAGE} {ELSE} getCatImageLink($subcategory.link_rewrite, $subcategory.id_image, 'medium')}" alt="" /> {/IF} {else} {/if} Link to comment Share on other sites More sharing options...
uddhava Posted February 23, 2010 Author Share Posted February 23, 2010 @rocky :If you would have a quick solution to get a product image to show as the subcategory image that would be nice.Your first quick solution did not work.What does the 0 mean in the code ? ($products.0.link_rewrite) Link to comment Share on other sites More sharing options...
rocky Posted February 24, 2010 Share Posted February 24, 2010 Sorry, I don't have a quick solution. It would take time for me to come up with a solution. To do that you want, you would have to pass not only the products of the current category, but also the products of the subcategories into product-list.tpl. Then you'll be able to randomly choose one of the subcategory product images.$products is an array of all the products in the current category. The 0 is the index of $products array. 0 is the first element in the products array. Link to comment Share on other sites More sharing options...
uddhava Posted February 26, 2010 Author Share Posted February 26, 2010 @rocky: i changed the code you gave. Instead of 'category' i used 'home'. That seems to work.I have added this code to the category.tpl: {if $subcategory.id_image} getImageLink($products.0.link_rewrite, $products.0.id_image, 'home')}" alt="{$products.0.name|escape:'htmlall':'UTF-8'}" title="{$products.0.name|escape:'htmlall':'UTF-8'}" id="categoryImage" /> {else} {/if} Now it displays the first product in the current (main) category.How will it display the product from the subcategory? Link to comment Share on other sites More sharing options...
uddhava Posted February 26, 2010 Author Share Posted February 26, 2010 I still was not happy with the result.So i have changed again the category templateNow it will only display images that i created myself.For example:I have a subcategory with ID 23.So i created a image with the name subcat-23.jpg and copied this image in the /img/c/ directory.Now the code in category.tpl will display this image.Your image can be sized as you like. The standard size is 129x129.Here is the code you need to change in your category.tpl (/themes/your-theme/)(starting from line 31) {if $subcategory.id_image} {else} {/if} Now it will display only that image that i want, and i can change the image whenever i need.Remember that subcategory ID needs to go into the image name and also the 'subcat' text.So for example : subcat-[subcategory ID].jpg like this "subcat-1.jpg" Link to comment Share on other sites More sharing options...
DonCarlos Posted December 24, 2010 Share Posted December 24, 2010 Sorry to bump the old thread.this is my first post but I managed to fix this issue!here is the code:category.php change this: $category->name = Category::hideCategoryPosition($category->name); $category->description = nl2br2($category->description); $subCategories = $category->getSubCategories(intval($cookie->id_lang)); $smarty->assign('category', $category); if (Db::getInstance()->numRows()) $smarty->assign('subcategories', $subCategories); To: $category->name = Category::hideCategoryPosition($category->name); $category->description = nl2br2($category->description); $smarty->assign('category_description_length', strlen($category->description)); $subCategories = $category->getSubCategories(intval($cookie->id_lang)); $smarty->assign('category', $category); if (Db::getInstance()->numRows()) $teller = 0; foreach ($subCategories as $subcatimg) { $fetchsubcatimg = mysql_fetch_assoc(mysql_query("SELECT * FROM `image` WHERE `id_product`=(SELECT `id_product` FROM `product` WHERE `id_category_default`='".$subcatimg['id_category']."' ORDER BY RAND() LIMIT 1) LIMIT 1")); array_push($subCategories[$teller], $fetchsubcatimg['id_product'].'-'.$fetchsubcatimg['id_image'].'-small.jpg'); $teller++; } $smarty->assign('subcategories', $subCategories); In category.tpl change: {if $subcategory.id_image} getCatImageLink($subcategory.link_rewrite, $subcategory.id_image, 'medium')}" alt="" /> {else} TO: {if $subcategory.id_image} {else} I am not that expierenced to make a good array_push in PHP so that you can make a custom value and not the 0 but it does work! hope it helped. Link to comment Share on other sites More sharing options...
shopimport.nl Posted September 1, 2011 Share Posted September 1, 2011 I managed to use the most recent product image for a category. Also when category doesn't have products but only subcategories. Of course one of the subcategories should have a product. This is tested on 1.4.4. Find the file controller/CategoryController.php and just behind: $this->category->description = nl2br2($this->category->description); $subCategories = $this->category->getSubCategories((int)(self::$cookie->id_lang)); self::$smarty->assign('category', $this->category); Add this piece of code to find the image. foreach($subCategories as &$subCategory ) { $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' select p.id_product, i.id_image from '._DB_PREFIX_.'category c1, '._DB_PREFIX_.'category c2 inner join '._DB_PREFIX_.'product p on (p.id_category_default = c2.id_category) inner join '._DB_PREFIX_.'image i on (i.id_product = p.id_product and i.cover = 1) where c2.nleft >= c1.nleft and c2.nleft <= c1.nright and c1.id_category = '.(int)$subCategory['id_category']. ' order by p.date_add desc limit 0,1'); if(isset($result[0] ) ) { $subCategory['productimage'] = $result[0]['id_product'] . '-'. $result[0]['id_image']; } } Then you can use in the category.tpl <img src="{$link->getImageLink($subcategory.link_rewrite, $subcategory.productimage, 'home')}" alt="{$products.0.name|escape:'htmlall':'UTF-8'}" title="{$products.0.name|escape:'htmlall':'UTF-8'}" id="categoryImage" /> This results in having automated category images by use of latest product image. Link to comment Share on other sites More sharing options...
platnumcn Posted September 9, 2011 Share Posted September 9, 2011 Hello, shopimport.nl, I did follow your instruction to edit the file on ps 1.44 not work, still no any image in my subcategory list. shopimport.nl I managed to use the most recent product image for a category. Also when category doesn't have products but only subcategories. Of course one of the subcategories should have a product. This is tested on 1.4.4. Find the file controller/CategoryController.php and just behind: $this->category->description = nl2br2($this->category->description); $subCategories = $this->category->getSubCategories((int)(self::$cookie->id_lang)); self::$smarty->assign('category', $this->category); Add this piece of code to find the image. foreach($subCategories as &$subCategory ) { $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' select p.id_product, i.id_image from '._DB_PREFIX_.'category c1, '._DB_PREFIX_.'category c2 inner join '._DB_PREFIX_.'product p on (p.id_category_default = c2.id_category) inner join '._DB_PREFIX_.'image i on (i.id_product = p.id_product and i.cover = 1) where c2.nleft >= c1.nleft and c2.nleft <= c1.nright and c1.id_category = '.(int)$subCategory['id_category']. ' order by p.date_add desc limit 0,1'); if(isset($result[0] ) ) { $subCategory['productimage'] = $result[0]['id_product'] . '-'. $result[0]['id_image']; } } Then you can use in the category.tpl <img src="{$link->getImageLink($subcategory.link_rewrite, $subcategory.productimage, 'home')}" alt="{$products.0.name|escape:'htmlall':'UTF-8'}" title="{$products.0.name|escape:'htmlall':'UTF-8'}" id="categoryImage" /> This results in having automated category images by use of latest product image. 1 Link to comment Share on other sites More sharing options...
leelee23 Posted October 2, 2011 Share Posted October 2, 2011 I tried the code highlighted by shopimport.nl but sadly, I'm experiencing no photos either Link to comment Share on other sites More sharing options...
markb Posted November 11, 2011 Share Posted November 11, 2011 Self edited: moved originally posted content to correct forum Link to comment Share on other sites More sharing options...
Blulight Design Posted December 1, 2012 Share Posted December 1, 2012 I managed to use the most recent product image for a category. Also when category doesn't have products but only subcategories. Of course one of the subcategories should have a product. This is tested on 1.4.4. Find the file controller/CategoryController.php and just behind: $this->category->description = nl2br2($this->category->description); $subCategories = $this->category->getSubCategories((int)(self::$cookie->id_lang)); self::$smarty->assign('category', $this->category); Add this piece of code to find the image. foreach($subCategories as &$subCategory ) { $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' select p.id_product, i.id_image from '._DB_PREFIX_.'category c1, '._DB_PREFIX_.'category c2 inner join '._DB_PREFIX_.'product p on (p.id_category_default = c2.id_category) inner join '._DB_PREFIX_.'image i on (i.id_product = p.id_product and i.cover = 1) where c2.nleft >= c1.nleft and c2.nleft <= c1.nright and c1.id_category = '.(int)$subCategory['id_category']. ' order by p.date_add desc limit 0,1'); if(isset($result[0] ) ) { $subCategory['productimage'] = $result[0]['id_product'] . '-'. $result[0]['id_image']; } } Then you can use in the category.tpl <img src="{$link->getImageLink($subcategory.link_rewrite, $subcategory.productimage, 'home')}" alt="{$products.0.name|escape:'htmlall':'UTF-8'}" title="{$products.0.name|escape:'htmlall':'UTF-8'}" id="categoryImage" /> This results in having automated category images by use of latest product image. This works just change: {if $subcategory.id_image} <img src="{$link->getCatImageLink($subcategory.link_rewrite, $subcategory.id_image, 'medium')}" alt="" width="{$mediumSize.width}" height="{$mediumSize.height}" /> {else} <img src="{$img_cat_dir}default-medium.jpg" alt="" width="{$mediumSize.width}" height="{$mediumSize.height}" /> {/if} to {if $subcategory.id_image != 'en-default'} <img src="{$link->getCatImageLink($subcategory.link_rewrite, $subcategory.id_image, 'medium')}" alt="" width="{$mediumSize.width}" height="{$mediumSize.height}" /> {else} <img src="{$link->getImageLink($subcategory.link_rewrite, $subcategory.productimage, 'medium')}" alt="" width="{$mediumSize.width}" height="{$mediumSize.height}" /> {/if} Link to comment Share on other sites More sharing options...
jeremieg Posted July 9, 2013 Share Posted July 9, 2013 hey people, sorry to wake this old thread, I'm actually coping with that situation and there's 2 things I don't understand from this thread: - does any of those tweaks work? - Blulight Design commented that it did work by adding some code somewhere. Could someone explain a little further? (like where to paste the code...) thank you!!! Link to comment Share on other sites More sharing options...
Blulight Design Posted October 3, 2013 Share Posted October 3, 2013 (edited) Updated for 1.5 - Create a CategoryController override and insert: /** * Assign sub categories templates vars AND get product image from subcategory */ protected function assignSubcategories() { if ($subCategories = $this->category->getSubCategories($this->context->language->id)) { foreach($subCategories as &$subCategory ) { $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' select p.id_product, i.id_image, pl.link_rewrite from '._DB_PREFIX_.'category c1, '._DB_PREFIX_.'category c2 inner join '._DB_PREFIX_.'product p on (p.id_category_default = c2.id_category) inner join '._DB_PREFIX_.'image i on (i.id_product = p.id_product and i.cover = 1) inner join '._DB_PREFIX_.'product_lang pl on (p.id_product = pl.id_product) where c2.nleft >= c1.nleft and c2.nleft <= c1.nright and c1.id_category = '.(int)$subCategory['id_category']. ' order by p.date_add desc limit 0,1'); if(isset($result[0] ) ) { $subCategory['productimage'] = $result[0]['id_product'] . '-'. $result[0]['id_image']; $subCategory['imglinkrewrite'] = $result[0]['link_rewrite']; } } $this->context->smarty->assign(array( 'subcategories' => $subCategories, 'subcategories_nb_total' => count($subCategories), 'subcategories_nb_half' => ceil(count($subCategories) / 2) )); } Then in category.tpl around line 79 replace for subcategory images: {if $subcategory.id_image != 'en-default'} <img src="{$link->getCatImageLink($subcategory.link_rewrite, $subcategory.id_image, 'medium_default')}" alt="" width="{$mediumSize.width}" height="{$mediumSize.height}" /> {else} <img src="{$link->getImageLink($subcategory.imglinkrewrite, $subcategory.productimage, 'medium_default')}" alt="" width="{$mediumSize.width}" height="{$mediumSize.height}" /> {/if} Edited October 3, 2013 by Blulight Design (see edit history) Link to comment Share on other sites More sharing options...
jodancer Posted March 26, 2015 Share Posted March 26, 2015 Hello, Anyone have this working on Prestashop 1.6? Link to comment Share on other sites More sharing options...
bibob Posted July 9, 2015 Share Posted July 9, 2015 Yes working on prestashop 1.6 also Create /override/controllers/front/CategoryController.php add <?php class CategoryController extends CategoryControllerCore { /** * Assign sub categories templates vars AND get product image from subcategory */ protected function assignSubcategories() { if ($subCategories = $this->category->getSubCategories($this->context->language->id)) { foreach($subCategories as &$subCategory ) { $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' select p.id_product, i.id_image, pl.link_rewrite from '._DB_PREFIX_.'category c1, '._DB_PREFIX_.'category c2 inner join '._DB_PREFIX_.'product p on (p.id_category_default = c2.id_category) inner join '._DB_PREFIX_.'image i on (i.id_product = p.id_product and i.cover = 1) inner join '._DB_PREFIX_.'product_lang pl on (p.id_product = pl.id_product) where c2.nleft >= c1.nleft and c2.nleft <= c1.nright and c1.id_category = '.(int)$subCategory['id_category']. ' order by p.date_add desc limit 0,1'); if(isset($result[0] ) ) { $subCategory['productimage'] = $result[0]['id_product'] . '-'. $result[0]['id_image']; $subCategory['imglinkrewrite'] = $result[0]['link_rewrite']; } } $this->context->smarty->assign(array( 'subcategories' => $subCategories, 'subcategories_nb_total' => count($subCategories), 'subcategories_nb_half' => ceil(count($subCategories) / 2) )); } } } add this to your theme/Category.tpl file {if $subcategory.id_image} <img src="{$link->getImageLink($subcategory.imglinkrewrite, $subcategory.productimage, 'medium_default')}" alt="" width="{$mediumSize.width}" height="{$mediumSize.height}" /> delete the file /cache/class_index.php 1 Link to comment Share on other sites More sharing options...
jodancer Posted July 19, 2015 Share Posted July 19, 2015 Thanks but this is not working for me in 1.6. I get a blank page. Any ideas? Link to comment Share on other sites More sharing options...
cyberespia Posted April 13, 2016 Share Posted April 13, 2016 Yes working on prestashop 1.6 also Create /override/controllers/front/CategoryController.php add <?php class CategoryController extends CategoryControllerCore { /** * Assign sub categories templates vars AND get product image from subcategory */ protected function assignSubcategories() { if ($subCategories = $this->category->getSubCategories($this->context->language->id)) { foreach($subCategories as &$subCategory ) { $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' select p.id_product, i.id_image, pl.link_rewrite from '._DB_PREFIX_.'category c1, '._DB_PREFIX_.'category c2 inner join '._DB_PREFIX_.'product p on (p.id_category_default = c2.id_category) inner join '._DB_PREFIX_.'image i on (i.id_product = p.id_product and i.cover = 1) inner join '._DB_PREFIX_.'product_lang pl on (p.id_product = pl.id_product) where c2.nleft >= c1.nleft and c2.nleft <= c1.nright and c1.id_category = '.(int)$subCategory['id_category']. ' order by p.date_add desc limit 0,1'); if(isset($result[0] ) ) { $subCategory['productimage'] = $result[0]['id_product'] . '-'. $result[0]['id_image']; $subCategory['imglinkrewrite'] = $result[0]['link_rewrite']; } } $this->context->smarty->assign(array( 'subcategories' => $subCategories, 'subcategories_nb_total' => count($subCategories), 'subcategories_nb_half' => ceil(count($subCategories) / 2) )); } } } add this to your theme/Category.tpl file {if $subcategory.id_image} <img src="{$link->getImageLink($subcategory.imglinkrewrite, $subcategory.productimage, 'medium_default')}" alt="" width="{$mediumSize.width}" height="{$mediumSize.height}" /> delete the file /cache/class_index.php For me works well in 1.6. But as it would be if I want to make the image of the product best-selling each subcategory 1 Link to comment Share on other sites More sharing options...
PAULGELO Posted October 11, 2016 Share Posted October 11, 2016 (edited) Hello, Thks, it's work for me PS1.6.1.7. Like cyberespia, is it possible to have a random image or image of product best-selling ? Actually, the override select the older product in the subcategory. I would to know the parameters I can modify. Thks, Paul ps : We can take the image of newest product. Change "order by p.date_add desc limit 0,1" to "order by p.date_add asc limit 0,1" Edited October 11, 2016 by PAULGELO (see edit history) Link to comment Share on other sites More sharing options...
pandazulweb Posted April 14, 2021 Share Posted April 14, 2021 Hi sorry to revive such an old post, but does anyone know how can we implement this into 1.7 ? I have tried without success. Link to comment Share on other sites More sharing options...
ru5hm3 Posted April 29, 2021 Share Posted April 29, 2021 (edited) if i delete this file /cache/class_index.php, will afect with something my store? or this file is rebuilding? atm my page is returning like this the results i want to use the code from cyberespia, now i must enter and add img in subcategory details for each subcategory and i want the img to be added randomly Thnx in advance for answer! Edited April 29, 2021 by ru5hm3 explain my error (see edit history) Link to comment Share on other sites More sharing options...
niekoarchimedes Posted April 29, 2021 Share Posted April 29, 2021 (edited) On 2/18/2010 at 5:42 PM, uddhava said: How do i get a random product image as the subcategory when there was no subcategory image used? I was looking around in the category.tpl file. It has to do with this code: {if $subcategory.id_image} getCatImageLink($subcategory.link_rewrite, $subcategory.id_image, 'medium')}" alt="" /> {else} {/if} How to get a random product pic, md restoration services or even only the first product pic in this category? I also looked in the product.tpl and found this code: img src="{$link->getImageLink($product.link_rewrite, $product.id_image, 'home')}" How to get the 2 mixed so we see a product image instead of the category image when none is defined? It would take time for me to come up with a solution. To do that you want, you would have to pass not only the products of the current category, but also the products of the subcategories into product-list.tpl. Edited April 30, 2021 by niekoarchimedes (see edit history) Link to comment Share on other sites More sharing options...
GrzegorzCh Posted June 7, 2023 Share Posted June 7, 2023 On 14.04.2021 at 03:05, pandazulweb said: Cześć, przepraszam, że odświeżam tak stary post, ale czy ktoś wie, jak możemy to zaimplementować w wersji 1.7? Próbowałem bez powodzenia. Też próbuję zrobić cos takiego ale w 1.7 niestety nie działa. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now