Sub Category Images (complete solution)

Ok so im kind of new here and i wanted this mod as much as everyone else and there have been some of you that have come up with some good ideas (hard coding of the sub cat. images with the category number from smarty) grabbing the image from the database and so on…however there has yet to be someone to post up how to do this for using the filesystem and remain dymanic…so here is the solution. im not sure how effecient this is but it works nevertheless…im still working on trying to get the image_path to be in the $subcategories array…its alot trickier than i thought it would be.



any questions just hit me up.




first edit the “core/fn_catalog.php” file



you want to add this function

```php //

// Get category image_path by its id

//

function fn_get_category_imagepath($category_id = 0)

{

global $db_tables;



if (!empty($category_id)) {

$image_id = db_get_field("SELECT image_id FROM $db_tables[category_images_links] WHERE category_id=‘$category_id’ ");

$image_id = db_get_field("SELECT image_path FROM $db_tables[category_images] WHERE image_id=‘$image_id’ ");

if (!empty($image_id)) {

return $image_id;

} else {

return “no_image.gif”;

}

}



return false;

} ```



i would put it just above this function



//

// Delete product by its id

//

function fn_delete_product($product_id)

next you want to edit your “categories.tpl” file



completely remove the entire {foreach}{/foreach} and all the lines inbetween it



then you want to paste this over




{foreach from=$subcategories item=category}
{assign var=darin value=$category.category_id}
{php}
$smarty_var = $this->get_template_vars('darin');
$cat_var = fn_get_category_imagepath($smarty_var);
$this->assign('category_image', $cat_var);
{/php}

{$category.category}
{if $category_image == "no_image.gif"}
{$category.category}
{else}
{$category.category}
{/if}


{/foreach}






of course you might want to edit that to your liking, but its the basics that will get you going

set your cs-cart to have the images on the filesystem and there you go, no_image still works and you get the exact thumbnail for every category that has one…working demo on [url]林芝诓勺餐饮管理有限公司

i guess if you wanted to get fancy, you could edit your categories.tpl file with the following instead and it will put everything in groups of 3…



{split data=$subcategories size=$columns|default:"3" assign="splitted_categories"}
{math equation="floor(100/x)" x=$columns|default:"3" assign="cell_width"}

{foreach from=$splitted_categories item="subcategories"}

{foreach from=$subcategories item="category"}
{if $category}


{assign var=darin value=$category.category_id}
{php}
$smarty_var = $this->get_template_vars('darin');
$cat_var = fn_get_category_imagepath($smarty_var);
$this->assign('category_image', $cat_var);
{/php}

{$category.category}
{if $category_image == "no_image.gif"}
{$category.category}
{else}
{$category.category}
{/if}



{else}

{/if}
{/foreach}

{/foreach}

[QUOTE]
{$category.category}
{if $category_image == "no_image.gif"}
{$category.category}
{else}
{$category.category}
{/if}

[/QUOTE]



I have added these changes, it works . . . kind of. It shows an image box for each subcategory, and the links work, but it’s not pulling my images for some reason.



I have attempted to modify the code with no success. Here are my changes:

{$category.category}


{if $category_image == "no_image.gif"}
{$category.category}
{else}
{$category.category}
{/if}





Anyone have any ideas?



Also . . . where would I specify a class to this code? I want to modify the defualt font and image border.

What is the source output for the image that doenst appear?

What? I’m not sure what you mean by source output. You can see what I am talking about here:



[url]http://www.ruffhousedogcare.com/cscart[/url]



Thanks for taking the time to reply.

Toys & Chews


Your image variable is wrong somewhere along the line.

I haven’t changed it from the default. All I did was add the code on this post.

Well could depend on your version of CS vs his.

I’m getting closer:


{$category.category}

{include file="common_templates/image.tpl" show_detailed='N' object_type="category" images=$category.main_pair no_ids=true}




This obviously has something to do with the last part (object type= and images=). If ANYONE knows what I’m missing here . . .

[quote name=‘ETInteractive’]Toys & Chews



Your image variable is wrong somewhere along the line.[/QUOTE]



After reading this post again . . . where is this image variable supposed to be stated?



I uploaded a pic for each category using the “Manage Categories” interface from admin. Shouldn’t that be stating the image source for me?



Am I making any sense? Here is the complete code:

```php

{* $Id: categories.tpl 2153 2006-10-03 06:07:12Z zeke $ *}



{capture name=“mainbox”}



{if $subcategories or $category_data.description}

{$category_data.description}








{foreach from=$subcategories item="category"}
{if $category}

{else}

{/if}
{/foreach}

{/foreach}







{split data=$subcategories size=$columns|default:"3" assign="splitted_categories"}
{math equation="floor(100/x)" x=$columns|default:"3" assign="cell_width"}

{foreach from=$splitted_categories item="subcategories"}


{assign var=darin value=$category.category_id}
{php}
$smarty_var = $this->get_template_vars('darin');
$cat_var = fn_get_category_imagepath($smarty_var);
$this->assign('category_image', $cat_var);
{/php}



{$category.category}

{include file="common_templates/image.tpl" show_detailed='N' object_type="category" images=$category.main_pair no_ids=true}









{/if}

{if $products}
{if $settings.Appearance.columns_in_products_list > 1}
{include file="products_pages/products_multicolumns.tpl" columns=$settings.Appearance.columns_in_products_list}
{elseif $settings.Appearance.advanced_products_list == 'Y' && $client_env.is_javascript == 'Y'}
{include file="products_pages/products_advanced.tpl" title=""}
{else}
{include file="products_pages/products.tpl" title=""}
{/if}

{elseif !$subcategories}

{$lang.text_no_products}


{/if}

{/capture}
{include file="common_templates/mainbox.tpl" title=$category_data.category content=$smarty.capture.mainbox}

{if $featured_products}
{include file="addons/featured_products/customer_categories.tpl"}
{/if}

{if $settings.Modules.discussion == 'Y'}
{include file="addons/discussion/discussion.tpl" object_id=$category_data.category_id object_type="C" title=$lang.discussion_title_category}
{/if} ```

[quote name=‘ETInteractive’]Toys & Chews



Your image variable is wrong somewhere along the line.[/QUOTE]



Isn’t this already specified for me in the image.tpl file:


{if $settings.General.images_location == "database"}
{assign var="image_src" value="$image_index?object_type=`$object_type`&image_id=`$images.image_id`"}
{else} {* image is stored in file system *}
{assign var="image_src" value=$images.icon.image_path|default:$no_image_path}
{/if}




This is why I have modified his code to remove the [COLOR=“Blue”]
Now I just have to specify what images to use . . . that’s the part I can’t figure out.



[COLOR=“Blue”]object_type=“category” images=$category.main_pair no_ids=true}[/COLOR]

what exactly are you trying todo…msg me and ill help you get this working…if you read correctly, i dont edit the image.tpl file…follow my directions 100%, im using 1.3.4 sp3

Having the same issues as tonybeam. Did yall get this worked out?

i dont fully understand the issues you are having…please send me a private msg and i will work with you to solve any issues you may have

[quote name=‘darinneagle’]i dont fully understand the issues you are having…please send me a private msg and i will work with you to solve any issues you may have[/QUOTE]



I did PM you after I posted the first time.

i am very unclear as to what you did wrong…because this has worked fine for me and several other users…

Thanks DarinnEagle, this works great. It should be there as standard.



For those who cannot get this to work, make sure you select an image to use for the subcategory,


  1. Goto Manage Categories
  2. Choose the sub category
  3. Choose the ICON image.
  4. Save changes.



    This is my Categories.tpl (complete)

```php

{* $Id: categories.tpl 2153 2006-10-03 06:07:12Z zeke $ *}



{capture name=“mainbox”}



{if $subcategories or $category_data.description}

{$category_data.description}





{*
*}

{foreach from=$subcategories item="category"}
{if $category}

{else}

{/if}
{/foreach}

{/foreach}



{include file="common_templates/image.tpl" show_detailed='Y' images=$category_data.main_pair object_type="category" no_ids=true}

{split data=$subcategories size=$columns|default:"3" assign="splitted_categories"}
{math equation="floor(100/x)" x=$columns|default:"3" assign="cell_width"}

{foreach from=$splitted_categories item="subcategories"}


{assign var=darin value=$category.category_id}
{php}
$smarty_var = $this->get_template_vars('darin');
$cat_var = fn_get_category_imagepath($smarty_var);
$this->assign('category_image', $cat_var);
{/php}
{$category.category}


{if $category_image == "no_image.gif"}
{$category.category}
{else}
{$category.category}
{/if}





{/if}

{if $products}
{if $settings.Appearance.columns_in_products_list > 1}
{include file="products_pages/products_multicolumns.tpl" columns=$settings.Appearance.columns_in_products_list}
{elseif $settings.Appearance.advanced_products_list == 'Y' && $client_env.is_javascript == 'Y'}
{include file="products_pages/products_advanced.tpl" title=""}
{else}
{include file="products_pages/products.tpl" title=""}
{/if}

{elseif !$subcategories}

{$lang.text_no_products}


{/if}

{/capture}
{include file="common_templates/mainbox.tpl" title=$category_data.category content=$smarty.capture.mainbox}

{if $featured_products}
{include file="addons/featured_products/customer_categories.tpl"}
{/if}

{if $settings.Modules.discussion == 'Y'}
{include file="addons/discussion/discussion.tpl" object_id=$category_data.category_id object_type="C" title=$lang.discussion_title_category}
{/if}
```

I've commented out the "standard category image" at the top of the page and added ' style="border: none" ' to the images so as not to get a blue/purple hyperlink boarder.