Author Topic: MOD: Random Category Images  (Read 13435 times)

Steve

  • Beginner
  • *
  • Posts: 31
MOD: Random Category Images
« on: August 18, 2008, 21:28:22 pm »
This mod will randomly choose a product image to use as the category image.  It will only choose an image if there isn't already a image set for the category. The image is chosen from any product within the category and includes products within sub categories. If there is a published category without any products there is no image shown for the category image. My mod for 1.0.x would show a noimage picture but this version doesn't show any image for a category with no products.

I'm using a joomlart virtuemart theme but this should work for others.

VirtueMart: 1.1.2
Joomla: 1.5.5

File: categoryChildlist.tpl.php
Location: /components/com_virtuemart/themes/**Theme**/templates/common/

Code: [Select]

After:

<?php
if ( $category["category_thumb_image"] ) {
echo ps_product::image_tag$category["category_thumb_image"], "alt=\"".$category["category_name"]."\""0"category");
echo "<br /><br/>\n";
}

Add:

              else {
        
    $category_a = array($category["category_id"]);
                
$db2 = new ps_DB;
                while(list(
$key,$val) = each($category_a)) {
                $q2 "SELECT category_child_id FROM #__{vm}_category_xref,#__{vm}_category";
                    
$q2 .= " WHERE #__{vm}_category_xref.category_parent_id=$val";
                    
$q2 .= " AND category_id=category_child_id AND category_publish='Y'";
                    
$db2->setQuery($q2);
                    
$db2->query();
                    while (
$db2->next_record()) {
                    array_push($category_a,$db2->f("category_child_id"));
                    }
                }

                
$product_a = array();
                foreach(
$category_a as $val) {
                    
$q2 "SELECT #__{vm}_product_category_xref.product_id as products";
                    
$q2 .= " FROM #__{vm}_product, #__{vm}_product_category_xref";
                    
$q2 .= " WHERE #__{vm}_product_category_xref.category_id=$val";
                    
$q2 .= " AND #__{vm}_product.product_id=#__{vm}_product_category_xref.product_id";
                    
$q2 .= " AND #__{vm}_product.product_publish='Y'";
                    
$db2->setQuery($q2);
                    
$db2->query();
                while ($db2->next_record()) {
                    array_push($product_a,$db2->f("products"));
                }
       }
if ($product_a) {
           $rand_key array_rand($product_a);
           $category_image $product_a[$rand_key];

        
       $q2 "SELECT product_thumb_image FROM #__{vm}_product";
        
       $q2 .= " WHERE product_id='".$category_image."' ";
        
       $db2->setQuery($q2);
        
       $db2->query();

            
   echo ps_product::image_tag$db2->f("product_thumb_image"), "alt=\"".$category["category_name"]."\""0"product");
            
   echo "<br /><br/>\n";
}
            }



erick_alcala

  • Jr. Member
  • **
  • Posts: 155
  • Everything is Possible!
    • Ofix-Comp
Re: MOD: Random Category Images
« Reply #1 on: August 22, 2008, 05:26:03 am »
Awesome Mod, thank you
wanting to learn to create good templates

zdjl505

  • Beginner
  • *
  • Posts: 18
    • roncekembang
Re: MOD: Random Category Images
« Reply #2 on: December 26, 2008, 17:14:53 pm »
Thanks Steve, it work on my site  ;D
The spirit of sharing

Ka lodger

  • Beginner
  • *
  • Posts: 4
Re: MOD: Random Category Images
« Reply #3 on: April 21, 2009, 04:00:23 am »
hi all..

here is the COMPLETE content of the vm template file, with 2 slight modifications:
the image and the link to a category contain now classes, so you can mdoify the apprearance easily via your template css.. best regards!
philipp
www.ninc.at


instructions: completly replace the contents of your categoryChildlist.tpl.php with that below (VM 1.1.3, Joomla 1.5.9) or extract differences between the version above and this and merge the changes in your file..

Code: [Select]
<?php if( !defined'_VALID_MOS' ) && !defined'_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
mm_showMyFileName(__FILE__);

$iCol 1;
if( !isset( 
$categories_per_row )) {
$categories_per_row 4;
}
$cellwidth intval100 $categories_per_row );

if( empty( 
$categories )) {
return; // Do nothing, if there are no child categories!
}
?>

<br/>
<table width="100%" cellspacing="0" cellpadding="0">
<?php
foreach( $categories as $category ) {
if ($iCol == 1) { // this is an indicator wether a row needs to be opened or not
echo "<tr>\n";
}
?>



<td align="center" width="<?php echo $cellwidth ?>%" >
<br />
         <a class="vmmainlink" title="<?php echo $category["category_name"?>" href="<?php $sess->purl(URL."index.php?option=com_virtuemart&amp;page=shop.browse&amp;category_id=".$category["category_id"]) ?>">
<?php
if ( $category["category_thumb_image"] ) {
echo ps_product::image_tag$category["category_thumb_image"], "alt=\"".$category["category_name"]."\""0"category");
echo "<br /><br/>\n";
}

  else {
             $category_a = array($category["category_id"]);
                $db2 = new ps_DB;
                while(list($key,$val) = each($category_a)) {
                $q2 "SELECT category_child_id FROM #__{vm}_category_xref,#__{vm}_category";
                    $q2 .= " WHERE #__{vm}_category_xref.category_parent_id=$val";
                    $q2 .= " AND category_id=category_child_id AND category_publish='Y'";
                    $db2->setQuery($q2);
                    $db2->query();
                    while ($db2->next_record()) {
                    array_push($category_a,$db2->f("category_child_id"));
                    }
                }

                $product_a = array();
                foreach($category_a as $val) {
                    $q2 "SELECT #__{vm}_product_category_xref.product_id as products";
                    $q2 .= " FROM #__{vm}_product, #__{vm}_product_category_xref";
                    $q2 .= " WHERE #__{vm}_product_category_xref.category_id=$val";
                    $q2 .= " AND #__{vm}_product.product_id=#__{vm}_product_category_xref.product_id";
                    $q2 .= " AND #__{vm}_product.product_publish='Y'";
                    $db2->setQuery($q2);
                    $db2->query();
                while ($db2->next_record()) {
                    array_push($product_a,$db2->f("products"));
                }
       }
if ($product_a) {
           $rand_key array_rand($product_a);
           $category_image $product_a[$rand_key];

                $q2 "SELECT product_thumb_image FROM #__{vm}_product";
                $q2 .= " WHERE product_id='".$category_image."' ";
                $db2->setQuery($q2);
                $db2->query();

                echo ps_product::image_tag$db2->f("product_thumb_image"), "class=\"vmmainimg01\" alt=\"".$category["category_name"]."\""0"product");
                echo "<br /><br/>\n";
}
            }


echo $category["category_name"];
echo $category['number_of_products'];
?>

</a><br/>
</td>


<?php
// Do we need to close the current row now?
if ($iCol == $categories_per_row) { // If the number of products per row has been reached
echo "</tr>\n";
$iCol 1;
}
else {
$iCol++;
}
}
// Do we need a final closing row tag?
if ($iCol != 1) {
echo "</tr>\n";
}
?>

</table>

fototbr

  • Beginner
  • *
  • Posts: 1
Re: MOD: Random Category Images
« Reply #4 on: April 22, 2009, 12:35:03 pm »
This mod works great! Thx for taking time to produce this !

fabry8080

  • Beginner
  • *
  • Posts: 6
Re: MOD: Random Category Images
« Reply #5 on: May 21, 2009, 11:00:04 am »
I have made the modification but I have a problem.

Many categories have the "product image unavailable" also when on their sub-category or sub-sub-category there are a lot of products with images associated.

Why?


erick_alcala

  • Jr. Member
  • **
  • Posts: 155
  • Everything is Possible!
    • Ofix-Comp
Re: MOD: Random Category Images
« Reply #6 on: October 06, 2009, 02:40:52 am »
is there a way of making this installable?
wanting to learn to create good templates

ajh

  • Beginner
  • *
  • Posts: 9
Re: MOD: Random Category Images
« Reply #7 on: April 14, 2010, 09:19:13 am »
Amazing.  I was thinking....man it would be nice to have images show up randomly based on the sub-category.

Googled it.

Found your post.

Perfect!

Thank you !!!

erick_alcala

  • Jr. Member
  • **
  • Posts: 155
  • Everything is Possible!
    • Ofix-Comp
Re: MOD: Random Category Images
« Reply #8 on: July 02, 2010, 07:47:00 am »
uhmm I just found that when you echo
Code: [Select]
echo ps_product::image_tag( $db2->f("product_thumb_image"));

it shows the following including a property border="0" which is annoying bc I can not validate Strict XHTML 1.1
wanting to learn to create good templates

erick_alcala

  • Jr. Member
  • **
  • Posts: 155
  • Everything is Possible!
    • Ofix-Comp
Re: MOD: Random Category Images
« Reply #9 on: July 02, 2010, 08:34:21 am »
Ok so I found out how to change this. but I quite dont like the solution, so the thing is image_tag is a class that automatically adds border=""

so you have to modify two files ps_product.php

under function
Code: [Select]
image_tag($image, $args="", $resize=1, $path_appendix='product', $thumb_width=0, $thumb_height=0 ) {
global $mosConfig_live_site, $mosConfig_absolute_path;
require_once( CLASSPATH . 'imageTools.class.php');

Comment out

Code: [Select]

/* $border="";
if( strpos( $args, "border=" )===false ) {
$border = '';
}
$height = $width = 0;*/


and then go to htmlTools.class.php and delete border=""

Code: [Select]

function imageTag( $src, $alt='', $align='', $height='', $width='', $title='', $attributes='' ) {


comment out the condition of the property border:

Code: [Select]
/*if( strpos($attributes, 'border=')===false) {
$border = ' border="'.$border.'"';
}*/ // Prevent doubled attributes

and on the code from theme.php you have to get rid of borders properties .... I will let that for you to figure out just do a search and delete =)


wanting to learn to create good templates

Doggiefield

  • 3rd party VirtueMart Developer
  • Jr. Member
  • *
  • Posts: 57
    • POS for Webshops
  • VirtueMart Version: 2 & 3
Re: MOD: Random Category Images
« Reply #10 on: April 17, 2011, 23:57:26 pm »
This seems to be the solution for my problem!

Is it also possible to create a "windows" like folder view? I attached an image to explain my question.

I want to use multiple product images to make sure every user knows this is a folder and not a product.
Is it possible to use this hack for something like this?


[attachment cleanup by admin]
Point of sale for Virtuemart
https://www.posforwebshops.com

PromoNet

  • Jr. Member
  • **
  • Posts: 93
Re: MOD: Random Category Images
« Reply #11 on: November 02, 2011, 11:49:57 am »
Very cool mod thanks!  Still works on 1.1.13 and joomla 1.5.23

One additional feature of setting them to the same image for each ip address/visitor would be good...  because you don't really want them to change on every page refresh as that will make navigation a pain.
Virtuemart for the Promotional Products Industry: http://www.promonet.co.uk/ (ProMart)

quaysideweb

  • Beginner
  • *
  • Posts: 1
Re: MOD: Random Category Images
« Reply #12 on: November 26, 2011, 19:33:01 pm »
Thanks for this - its perfect - just added 2800 products / 300+categories and no extra work  :)

webs4travel

  • Beginner
  • *
  • Posts: 33
Re: MOD: Random Category Images
« Reply #13 on: February 07, 2012, 11:30:00 am »
Great Mod - still work on 1.5.25 :-)

AndrewBucklin

  • Beginner
  • *
  • Posts: 13
Re: MOD: Random Category Images
« Reply #14 on: February 10, 2012, 19:49:27 pm »
I have made the modification but I have a problem.

Many categories have the "product image unavailable" also when on their sub-category or sub-sub-category there are a lot of products with images associated.

Why?

To fix the "Product Image Unavailable", change the following line of the new code from the first post in this thread:
Code: [Select]
$q2 .= " AND category_id=category_child_id AND category_publish='Y'";
to this:
Code: [Select]
$q2 .= " WHERE product_thumb_image IS NOT NULL AND product_id='".$category_image."' ";