Author Topic: Use product image as category image  (Read 6939 times)

jabba

  • Beginner
  • *
  • Posts: 37
    • Pixed - Web Store Assistance
Use product image as category image
« on: June 05, 2013, 10:47:31 am »
Hello everyone! First of all i'm NOT a php or mysql guru, i'm kinda a novice so my code can be bad, slow and long as hell. But it do the tricks :-)

What i do not want: place an immage to every single category
Wha i do want: let the category pick an image from an his own product

how does this trick works? Some calls to the VM database and a lot of "for" cycles

Version 0.1beta: Works like a charm in a brand new virtuemart installed in a brand new joomla (tested on joomla 2.5.11, VM 2.0.20b)

NOT WORKING: if you do not have any product with images the category will show the classic "image not founded", even if you have a subcategory with products inside. So, this hack can't take images from annidiated subcategories

How to install it:

You have to put the following code inside any VM file that handle categories display:

HOMEPAGE: components/com_virtuemart/views/virtuemart/tmpl/default.php

INNER CATEGORIES: components/com_virtuemart/views/category/tmpl/default.php



IMPORTANT: BACKUP YOUR FILES FIRST!!!



Let's take the homepage as example.

- Open default.php

Go to this line of code

Code: [Select]
    <?php
    
// Start the Output
    
foreach ($this->categories as $category) {

after that

insert this:

Code: [Select]
//inizialize the image variable
$category_image_ok = "/components/com_virtuemart/assets/images/vmgeneral/noimage.gif";

//setup the first query
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select(array('virtuemart_product_id'));
$query->from('#__virtuemart_product_categories');
$query->where('virtuemart_category_id = "'.$category->virtuemart_category_id.'"');
$query->order('ordering ASC');
$db->setQuery($query);
$results = $db->loadObjectList();

//if i have some products inside the category then go on
if ($results){
foreach($results as $result)
{
//setup the 2nd query
$db2 = JFactory::getDbo();
$query2 = $db2->getQuery(true);
$query2->select(array('virtuemart_media_id'));
$query2->from('#__virtuemart_product_medias');
$query2->where('virtuemart_product_id = "'.$result->virtuemart_product_id.'"');
$query2->order('ordering ASC');
$db2->setQuery($query2);
$results2 = $db2->loadObjectList();

//if i have some media in the first product then go on
if ($results2){
foreach($results2 as $result2)
{
//last query: get the url of the media
$db3 = JFactory::getDbo();
$query3 = $db3->getQuery(true);
$query3->select(array('file_url_thumb'));
$query3->from('#__virtuemart_medias');
$query3->where('virtuemart_media_id = "'.$result2->virtuemart_media_id.'"');
$db3->setQuery($query3);
$results3 = $db3->loadObjectList();

//if i have medias then go on
if ($results3){
foreach($results3 as $result3)
{
//if i actually have a media file, an image
if ($result3->file_url_thumb){
$category_image_ok = $result3->file_url_thumb;
break;
}else{
//if i don't have an image then use the default "no image"
$category_image_ok = "/components/com_virtuemart/assets/images/vmgeneral/noimage.gif";
}
}
}
break;
}
}
}
}

then, replace this line:

Code: [Select]
echo $category->images[0]->displayMediaThumb("", false);
with this:

Code: [Select]
echo '<img src="'.$category_image_ok.'"></img>';
result:

      
Code: [Select]
<?php
    if (!empty(
$category->images)) {
echo '<img src="'.$category_image_ok.'"></img>';
    }
    ?>

how to test it?

Go to administration panel -> components -> virtuemart -> categories and delete all the virtuemart categories images

Example: http://www.pixed.it/catalogo2/accessori2013-05-21-03-17-21120340479.html

Hope this helps! If some of you can make this a bit more elegant it would be great. Any advice is appriciated

Bye!
Gianluca Gabella - CEO & Web Developer @ Pixed s.n.c.b - www.pixed.it

jabba

  • Beginner
  • *
  • Posts: 37
    • Pixed - Web Store Assistance
Re: Use product image as category image
« Reply #1 on: June 05, 2013, 10:51:41 am »
ok, did i put this in the VM 1.1 section? how can i move it to the VM2 section? Thanks!
Gianluca Gabella - CEO & Web Developer @ Pixed s.n.c.b - www.pixed.it

mmgr

  • Beginner
  • *
  • Posts: 1
Re: Use product image as category image
« Reply #2 on: July 31, 2013, 19:35:01 pm »
Very Helpful Thanks a lot for the tip.

It would be great if it could also, drill down to child category and find a product that has image and to assign it to its parent category, if it doesn't have products, instead of the noimage.gif

That would make this customization fantastic.