VirtueMart Forum

VirtueMart Dev/Coding Central: VM1 (old version) => Development Projects, Modifications, Hacks & Tweaks. VM1.1 => Topic started by: jabba on June 05, 2013, 10:47:31 am

Title: Use product image as category image
Post by: jabba 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


Let's take the homepage as example.

- Open default.php

Go to this line of code

Code: [Select]
// 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->where('virtuemart_category_id = "'.$category->virtuemart_category_id.'"');
$query->order('ordering ASC');
$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->where('virtuemart_product_id = "'.$result->virtuemart_product_id.'"');
$query2->order('ordering ASC');
$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->where('virtuemart_media_id = "'.$result2->virtuemart_media_id.'"');
$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;
//if i don't have an image then use the default "no image"
$category_image_ok = "/components/com_virtuemart/assets/images/vmgeneral/noimage.gif";

then, replace this line:

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

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

Code: [Select]
    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


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

Title: Re: Use product image as category image
Post by: jabba 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!
Title: Re: Use product image as category image
Post by: mmgr 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.