How To: Create An All Top-Level Cats W/Images Panel
Posted: Thu Jan 12, 2012 11:47 pm
I wanted a category listing just like the one found in the middle of the front page at http://www.bhphotovideo.com. It's awesome and should be a basic feature for this expensive cart. But it isn't. Sooooo after much pain and suffering (*smile*) I figured out a way to get a "best-I-can-do" version. My version lets you create a panel that can be added to the center of your home page that displays all top-level categories with images. Say whaaaaaat? Yep. 
Making the panel:
I used the sub categories feature found in categoryheading.php to get this thing going after many failed attempts at other methods.
In /includes/display/ create a file called: HomeCategories.php and inside of it paste this:
And in /templates/yourtemplatename/Panels/ create a new file called: HomeCategories.html and paste this in it:
In /templates/yourtemplatename/Snippets/ you are going to be creating 4 files. Create a file called: CatItem.html and paste this in it:
Then create a file called: CatItemImage.html and paste this in it:
Then create a file called: HomeCategoriesGrid.html and paste this in it:
Then create a file called: HomeCategories.html and paste this in it:
Then use this code where you want the panel to show up:
Now you will probably have a link list of all of your top level categories wherever you put this code. The next step below is to help you add images to this panel for each category. When you do, the text links will still be there as well. I'm thinking of removing those.
Tricking the script to get the images:
This part was quite infuriating. If only sucker punches could be sent via mail.
Our dear sweet friends who created ISC thought it would be nice to hide the image upload box for top level categories when you create a new top level cat. It magically appears when the new cat you are creating is a subcategory. I burned my eyes out trying to figure out how to get the image upload box to show up when you are creating a top level cat but I couldn't figure it out and I gave up. BUT what you can do is remove the code that forcefully removes the top-level category images (it does this when ever you create or edit a new category). Once you remove that bit of code, you have to create or edit a category and make it a subcategory so you can get the upload image box. Once you upload the image and save the category, you then have to move it so it's a top level category. With the code to delete top level cat images gone, it won't delete the images and you will now have a top level category with an image for your lovely new top-level category panel.
In admin/includes/classes/class.category.php locate the following code and delete it.
Also find and delete this in the same file:
Also in admin/includes/class.remote.php you can also delete this:
You're on your own from here and you'll have to style your new panel and make tweaks however you like. Cheers! Sharing is caring!

Making the panel:
I used the sub categories feature found in categoryheading.php to get this thing going after many failed attempts at other methods.

In /includes/display/ create a file called: HomeCategories.php and inside of it paste this:
Code: Select all
<?php
CLASS ISC_HOMECATEGORIES_PANEL extends PANEL
{
public function SetPanelSettings()
{
$GLOBALS['ISC_CLASS_CATEGORY'] = GetClass('ISC_CATEGORY');
if ($GLOBALS['EnableSEOUrls'] == 1) {
$baseLink = sprintf("%s/categories", $GLOBALS['ShopPath']);
} else {
$baseLink = sprintf("%s/categories.php?category=", $GLOBALS['ShopPath']);
}
// Output Top-level Categories
$GLOBALS['SNIPPETS']['HomeCategories'] = "";
$query = sprintf("select * from [|PREFIX|]categories where catparentid='0' and catvisible=1 order by catsort asc, catname asc", $GLOBALS['ISC_CLASS_DB']->Quote($GLOBALS['CatId']));
$result = $GLOBALS['ISC_CLASS_DB']->Query($query);
if($GLOBALS['ISC_CLASS_DB']->CountResult($result) > 0) {
// Check to see if we need to add in place holder images or if we are just displaying text
if (!($rtn = $GLOBALS['ISC_CLASS_DB']->Fetch($GLOBALS['ISC_CLASS_DB']->Query("SELECT COUNT(*) AS Total FROM [|PREFIX|]categories WHERE catimagefile != ''"))) || !$rtn['Total']) {
$useImages = false;
} else {
$useImages = true;
if (GetConfig('CategoryDefaultImage') !== '') {
$defaultImage = GetConfig('ShopPath') . '/' . GetConfig('CategoryDefaultImage');
} else {
$defaultImage = $GLOBALS['IMG_PATH'].'/CategoryDefault.gif';
}
}
$i = 0;
while ($row = $GLOBALS['ISC_CLASS_DB']->Fetch($result)) {
$i++;
if (!CustomerGroupHasAccessToCategory($row['categoryid'])) {
continue;
}
$GLOBALS['HomeCatName'] = isc_html_escape($row['catname']);
$GLOBALS['HomeCatLink'] = CatLink($row['categoryid'], $row['catname']);
if ($useImages) {
if ($row['catimagefile'] !== '') {
$GLOBALS['CatImage'] = GetConfig('ShopPath') . '/' . GetConfig('ImageDirectory') . '/' . $row['catimagefile'];
} else {
$GLOBALS['CatImage'] = $defaultImage;
}
$GLOBALS['ISC_CLASS_TEMPLATE']->assign('width', getConfig('CategoryImageWidth'));
$GLOBALS['ISC_CLASS_TEMPLATE']->assign('height', getConfig('CategoryImageHeight') + 50);
$GLOBALS['SNIPPETS']['HomeCategories'] .= $GLOBALS['ISC_CLASS_TEMPLATE']->GetSnippet("CatItemImage");
if ($i % GetConfig('CategoryPerRow') == 0) {
$GLOBALS['SNIPPETS']['HomeCategories'] .= '<li class="RowDivider" style="float:none; clear:both;"></li>';
}
} else {
$GLOBALS['SNIPPETS']['HomeCategories'] .= $GLOBALS['ISC_CLASS_TEMPLATE']->GetSnippet("CatItem");
}
}
if ($useImages) {
if ($i % GetConfig('CategoryPerRow') > 0) {
$GLOBALS['SNIPPETS']['HomeCategories'] .= '<li style="float: none; clear: both;"/>';
}
$output = $GLOBALS['ISC_CLASS_TEMPLATE']->GetSnippet("HomeCategoriesGrid");
} else {
$output = $GLOBALS['ISC_CLASS_TEMPLATE']->GetSnippet("HomeCategories");
}
$output = $GLOBALS['ISC_CLASS_TEMPLATE']->ParseSnippets($output, $GLOBALS['SNIPPETS']);
$GLOBALS['SNIPPETS']['HomeCategories'] = $output;
}
}
}
Code: Select all
<div class="Block Moveable Panel" id="CategoryHeading">
<div class="BlockContent">
%%SNIPPET_HomeCategories%%
</div>
</div>
Code: Select all
<li><a href="%%GLOBAL_HomeCatLink%%">%%GLOBAL_HomeCatName%%</a></li>
Code: Select all
<li style="width: %%GLOBAL_width%%px; height: %%GLOBAL_height%%px;">
<a href="%%GLOBAL_HomeCatLink%%" style="text-decoration: none;"><img src="%%GLOBAL_CatImage%%" border="0" /></a><br /><a href="%%GLOBAL_HomeCatLink%%">%%GLOBAL_HomeCatName%%</a>
</li>
Code: Select all
<div class="SubCategoryListGrid">
<ul>
%%SNIPPET_HomeCategories%%
</ul>
</div>
Code: Select all
<div class="SubCategoryList">
<ul>
%%SNIPPET_HomeCategories%%
</ul>
</div>
Code: Select all
%%Panel.HomeCategories%%
Tricking the script to get the images:
This part was quite infuriating. If only sucker punches could be sent via mail.

In admin/includes/classes/class.category.php locate the following code and delete it.
Code: Select all
// Also forcefully delete the image if it is a root category
if ($catData['category'] == "0") {
$this->DelCategoryImage($this->categoryAPI->catimagefile);
$catImage = '';
}
Code: Select all
public function RemoveRootImages()
{
$result = $GLOBALS['ISC_CLASS_DB']->Query("SELECT * FROM [|PREFIX|]categories WHERE catparentid='0' AND catimagefile != ''");
while ($row = $GLOBALS['ISC_CLASS_DB']->Fetch($result)) {
$this->DelCategoryImage($row['categoryid']);
$GLOBALS['ISC_CLASS_DB']->UpdateQuery('categories', array('catimagefile' => ''), "categoryid='" . (int)$row['categoryid'] . "'");
}
}
Code: Select all
// Also make sure that all the root categories do NOT have any images assoiated with them
$GLOBALS['ISC_CLASS_ADMIN_CATEGORY'] = GetClass('ISC_ADMIN_CATEGORY');
$GLOBALS['ISC_CLASS_ADMIN_CATEGORY']->RemoveRootImages();
You're on your own from here and you'll have to style your new panel and make tweaks however you like. Cheers! Sharing is caring!
