Zen Cart Logo
Forums / Setting Up Categories, Products, Attributes / Automatically populate category images from their product images

Automatically populate category images from their product images

Locked

Views: 8,403

Results 1 to 20 of 48
This thread is locked. New replies are disabled.
31 May 2010, 6:28 PM
#1
jackie_taferner avatar

jackie_taferner

Totally Zenned

Join Date:
Jun 2009
Location:
Orange County, California
Posts:
547
Plugin Contributions:
3

Automatically populate category images from their product images

It's quite tedious to add category images one by one by hand!

It would be very helpful to automatically pull the category images from the product photos that are contained within.

I've searched for modules to do this, and have found none. Anyone have any ideas how to tackle the custom coding?

1 Jun 2010, 1:04 PM
#2
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: Automatically populate category images from their product images

Usually a Category has many Products and the Products have 1 to many images ...

How would you determine from the Products which image should be used for the whole category? :unsure:

1 Jun 2010, 3:50 PM
#3
jackie_taferner avatar

jackie_taferner

Totally Zenned

Join Date:
Jun 2009
Location:
Orange County, California
Posts:
547
Plugin Contributions:
3

Re: Automatically populate category images from their product images

By default, i could use the main image from the first product in the category.

So, for example my product that has sort order "1", and has 3 images:

101.jpg <-- use this one
101_2.jpg
101_3.jpg

It would be even neater if the product could be specified in the admin:

Product to display for categories images (by sort order #): 1

But for me, this is actually not necessary because my products within each subcategory all have the same main image.... ANY product from the category will do fine :)

I'm just thinking to make it scalable and put into a module for others to use.

1 Jun 2010, 4:11 PM
#4
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: Automatically populate category images from their product images

How often are you making categories that you cannot just tell it the categories image when you set the category image then?

Or does the Product image for the category change that often that you need to update the category image?

I am having trouble picturing how a site would be setup where the Categories image is the same as the Product image and all the Products in a Category would use the same image ... :unsure:

1 Jun 2010, 4:16 PM
#5
jackie_taferner avatar

jackie_taferner

Totally Zenned

Join Date:
Jun 2009
Location:
Orange County, California
Posts:
547
Plugin Contributions:
3

Re: Automatically populate category images from their product images

Well, I'm using easy populate which is quite easy to name all the product images and simply upload to db. Unfortunately the category images are not so simple.

Manually adding the images by hand to every category is such a chore. This particular store doesn't have an insane number of categories, but it would be so much easier for it to auto-pull the images based on the products inside.

I know there are lots of others who are in a worse spot than me - they have thousands of products and categories, and would spend upwards of 10-20 hours just uploading category images.

That's not very efficient. I'd like to make a solution for everyone that can be put in modules section. I imagine it wouldn't be too tough - just a bit of editing to the product-listing.php template to pull a different image from the db?

1 Jun 2010, 4:24 PM
#6
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: Automatically populate category images from their product images

In the Developers Tool Kit ... if you do a search on:
categories_image

you can see all the places referenced and you can check to see if you can just change a simple template or module or perhaps the function for your needs on this ...

This is pretty specialized as I have not had too many people who need this ...

You could also customize a simple routine to be run when you update your products to go rebuild the categories table with the correct image from the products table based on the master_categories_id (assuming that is correct) ...

1 Jun 2010, 4:30 PM
#7
jackie_taferner avatar

jackie_taferner

Totally Zenned

Join Date:
Jun 2009
Location:
Orange County, California
Posts:
547
Plugin Contributions:
3

Re: Automatically populate category images from their product images

Ok, I will see what I can find tonight - thanks for the info!

I'm sure I'll have more questions once I dive into it :)

21 Aug 2010, 12:27 PM
#8
webmc avatar

webmc

New Zenner

Join Date:
Jul 2010
Location:
Kent UK
Posts:
50
Plugin Contributions:
0

Re: Automatically populate category images from their product images

I would like this function also - I have just uploaded 13000 products which lie in 300 categories, each of which require an image. I will have to look into it.....

21 Aug 2010, 4:09 PM
#9
webmc avatar

webmc

New Zenner

Join Date:
Jul 2010
Location:
Kent UK
Posts:
50
Plugin Contributions:
0

Re: Automatically populate category images from their product images

while (!$categories->EOF) {

$cat_image = $categories->fields['categories_name'] . ".jpg";
$filename = '/path/to/' . $cat_image;

if (file_exists($filename)) {
$cat_image = $categories->fields['categories_name'] . ".jpg";
} else {
$cat_image = "pixel_trans.gif";
}

    if (!$categories->fields['categories_image']) !$categories->fields['categories_image'] = $cat_image;

You could use this code in your category_row.php - This looks for a image matching the categories name - if the file exists it will show it....

/path/to/ - should be same as what is in your config files

21 Aug 2010, 4:23 PM
#10
webmc avatar

webmc

New Zenner

Join Date:
Jul 2010
Location:
Kent UK
Posts:
50
Plugin Contributions:
0

Re: Automatically populate category images from their product images

instead of /path/to

$filename = DIR_WS_IMAGES . $cat_image;

more of a zen cart way..... no changes needed then

not a fully grown solution to the problem but might be a quicker way to bulk upload category images, just make sure the category image you want to use matches the category name and have them in your image folder.

22 Aug 2010, 6:45 PM
#11
webmc avatar

webmc

New Zenner

Join Date:
Jul 2010
Location:
Kent UK
Posts:
50
Plugin Contributions:
0

Re: Automatically populate category images from their product images

The following code will display a random category image based on on products from that category - I am aware the if statement could be condensed

global $db;
$sql = "select products_image from " . TABLE_PRODUCTS . " where master_categories_id = " . $categories->fields['categories_id'] . " ORDER BY RAND() LIMIT 1";
$result = $db->Execute($sql);

$cat_image = $result->fields['products_image'];
$filename = DIR_WS_IMAGES . $cat_image;

if (file_exists($filename)) {

} else {

$cat_image = "pixel_trans.gif";
}

    if (!$categories->fields['categories_image']) !$categories->fields['categories_image'] = $cat_image;
22 Aug 2010, 6:59 PM
#12
webmc avatar

webmc

New Zenner

Join Date:
Jul 2010
Location:
Kent UK
Posts:
50
Plugin Contributions:
0

Re: Automatically populate category images from their product images

$sql = "select products_image from " . TABLE_PRODUCTS . " where master_categories_id = " . $categories->fields['categories_id'] . " AND products_image IS NOT NULL AND products_image != '.jpg' AND products_image != ' ' ORDER BY RAND() LIMIT 1";

you could change the sql to that to decrease the chances of it finding an empty image.......

I am tempted to look into a way for the if statement to repeat the lookup if no file exists but this might be dangerous if no products have images in the category.....

24 Aug 2010, 8:52 PM
#13
webmc avatar

webmc

New Zenner

Join Date:
Jul 2010
Location:
Kent UK
Posts:
50
Plugin Contributions:
0

Re: Automatically populate category images from their product images

If like my you were lucky enough to be planning ahead and have all your content ready and all the manufacturers images are convieniently already called the same as their category name and are jpgs, you could use sql to populate the fields with the path like so... (either in sql patches or phpmyadmin)

update manufacturers set manufacturers_image =concat('manufacturers/',manufacturers_name,'.jpg')

You have done this for the above also if you didnt want to play around with the code...

(just make sure you have images in you manufactuers folder in the image folder called the same as the manufatuer name)

8 Sep 2010, 3:56 PM
#14
sarahl avatar

sarahl

Zen Follower

Join Date:
May 2004
Location:
UK
Posts:
471
Plugin Contributions:
0

Re: Automatically populate category images from their product images

I too would find a mod like this handy - for clients that update their stock and don't want out of date category images displayed - basically showing the first image for the first product into the category would be perfect!

Did you get any progress on this?

9 Sep 2010, 7:59 PM
#15
mikestaps avatar

mikestaps

Zen Follower

Join Date:
Aug 2010
Location:
Grand Blanc, MI
Posts:
305
Plugin Contributions:
1

Re: Automatically populate category images from their product images

This definitely looks like something I would like to implement... just not 100% sure where I place this code?

WebMC:

The following code will display a random category image based on on products from that category - I am aware the if statement could be condensed

global $db;
$sql = "select products_image from " . TABLE_PRODUCTS . " where master_categories_id = " . $categories->fields['categories_id'] . " ORDER BY RAND() LIMIT 1";
$result = $db->Execute($sql);

$cat_image = $result->fields['products_image'];
$filename = DIR_WS_IMAGES . $cat_image;

if (file_exists($filename)) {

} else {

$cat_image = "pixel_trans.gif";
}

  if (!$categories->fields['categories_image']) !$categories->fields['categories_image'] = $cat_image;
9 Sep 2010, 8:47 PM
#16
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Automatically populate category images from their product images

This will use images named for each category just by uploading them; no individual admin work needed.

Edit /includes/modules/your_template/category_row.php.
Find```php
while (!$categories->EOF) {
if (!$categories->fields['categories_image']) !$categories->fields['categories_image'] = 'pixel_trans.gif';

and replace with```php
  while (!$categories->EOF) {
    $cat_image =  file_exists(DIR_WS_IMAGES . 'categoryimg' . $categories->fields['categories_id'] . '.jpg')? 'categoryimg' . $categories->fields['categories_id'] . '.jpg': 'pixel_trans.gif';
    if (!$categories->fields['categories_image']) !$categories->fields['categories_image'] = $cat_image;

Name the images like categoryimg23.jpg (replacing 23 with the category's id) and save in /images/.

Using the first product's image is more complicated unless it can be guaranteed that they will all be .jpg (or another fixed type).

9 Sep 2010, 9:01 PM
#17
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Automatically populate category images from their product images

WebMC's randomizing code could also be put in the same place:```php
while (!$categories->EOF) {
global $db;
$sql = "select products_image from " . TABLE_PRODUCTS . " where master_categories_id = " . $categories->fields['categories_id'] . " ORDER BY RAND() LIMIT 1";
$result = $db->Execute($sql);
$cat_image = $result->fields['products_image'];
$cat_image = file_exists(DIR_WS_IMAGES . $cat_image)? $cat_image: 'pixel_trans.gif';
if (!$categories->fields['categories_image']) !$categories->fields['categories_image'] = $cat_image;

9 Sep 2010, 9:15 PM
#18
mikestaps avatar

mikestaps

Zen Follower

Join Date:
Aug 2010
Location:
Grand Blanc, MI
Posts:
305
Plugin Contributions:
1

Re: Automatically populate category images from their product images

Thanks :smile:> gjh42:

WebMC's randomizing code could also be put in the same place:

9 Sep 2010, 9:24 PM
#19
mikestaps avatar

mikestaps

Zen Follower

Join Date:
Aug 2010
Location:
Grand Blanc, MI
Posts:
305
Plugin Contributions:
1

Re: Automatically populate category images from their product images

The code just killed my subcategory links.... every time you click on a subcategory you jump to the store's home page. It worked great for the random photo, but no go... :frusty:

  while (!$categories->EOF) {
    global $db;
    $sql = "select products_image from " . TABLE_PRODUCTS . " where master_categories_id = " . $categories->fields['categories_id'] . " ORDER BY RAND() LIMIT 1";
    $result = $db->Execute($sql);
    $cat_image = $result->fields['products_image'];
    $cat_image = file_exists(DIR_WS_IMAGES . $cat_image)? $cat_image: 'pixel_trans.gif';
    if (!$categories->fields['categories_image']) !$categories->fields['categories_image'] = $cat_image;
9 Sep 2010, 9:29 PM
#20
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: Automatically populate category images from their product images

If we can see it in action, perhaps we can tell what is going wrong.