Искал, как сделать дерево категорий с товарами на главной странице (shop.index) — не нашел, решил написать сам:
Для сворачивания/разворачивания дерева использовал скрипт
mkTreeПервое, что необходимо сделать, это установить сам mkTree
отсюдаСоответственно кладем mktree.js в папку с js'ами темы, mktree.css в папку со стилями и plus.gif, minus.gif и bullet.gif в папку с картинками темы.
Соответственно пути в mktree.css к картинкам необходимо поменять (у меня пути заменились на ../images/plus.gif и т.д.)
Включаем mktree.css в index.php вашей темы
<link href="<?php echo $mosConfig_live_site?>/templates/ВАША_ТЕМА/css/mktree.css" rel="stylesheet" type="text/css" />
И правим файл
administrator/components/com_virtuemart/html/shop.index.php:
Удаляем (или закомментируем) код от
<table width="100%" cellspacing="0" cellpadding="0">
до
и после
добавляем следующий код:
<script src="<?echo $mosConfig_live_site?>/templates/ВАШ_ШАБЛОН/js/mktree.js" language="javascript"></script>
<a href="#" onClick="expandTree('tree'); return false;">Развернуть список</a>
<a href="#" onClick="collapseTree('tree'); return false;">Свернуть список</a>
<?
function isParent ($cat_id, $cat_array) {
foreach ($cat_array as $cid => $cat) {
if ($cat['category_parent_id'] == $cat_id && $cat_id != $cid)
return TRUE;
}
return FALSE;
}
function isParentOf ($cat_parent, $cat_child, $cat_array) {
if ($cat_array[$cat_child]['category_parent_id'] == $cat_parent)
return TRUE;
return FALSE;
}
function isChild ($cat_id, $cat_array) {
if ($cat_array[$cat_id]['category_parent_id'] != 0)
return TRUE;
return FALSE;
}
function category_childlist ($cat_id, $cat_array) {
$childlist = array();
if (!isParent ($cat_id, $cat_array)) array();
foreach ($cat_array as $cid => $cat){
if (isParentOf ($cat_id, $cid, $cat_array)) {
if (isParent ($cid, $cat_array)) {
$childlist[$cid] = category_childlist ($cid, $cat_array);
}
else {
$childlist[$cid] = array();
}
}
}
return $childlist;
}
function category_tree ($cat_id, $cat_array) {
global $db,$sess;
$html = '';
$childlist = category_childlist ($cat_id, $cat_array);
if ($cat_id == 0) $html .= "<ul class=\"mktree\" id=\"tree\">\r\n";
else $html .= "<ul>\r\n";
foreach ($childlist as $cid => $cat) {
$html .= "<li><a href=\"". $sess->url(URL."index.php?option=com_virtuemart&page=shop.browse&category_id=".$cid) ."\">" . shopMakeHtmlSafe($cat_array[$cid]['category_name']) ."</a>\r\n";
if ($cat) $html .= category_tree ($cid, $cat_array);
if (trim (ps_product_category::products_in_category ($cid), " ()") != 0) {
$html .= "<ul>\r\n";
$q = "SELECT p.product_id,p.product_name "
." FROM #__{vm}_product p "
."\n LEFT JOIN #__{vm}_product_category_xref x ON x.product_id=p.product_id ";
$q .= " WHERE x.category_id='$cid' ";
$db->query ($q);
while ($db->next_record()) {
$html .= "<li><a href=\"".$sess->url(URL."index.php?option=com_virtuemart&page=shop.product_details&product_id=". $db->f("product_id"))."\">".$db->f("product_name")."</a></li>\r\n";
}
$html .= "</ul>\r\n";
}
$html .= "</li>\r\n";
}
$html .= "</ul>\r\n";
return $html;
}
$arr = ps_product_category::getCategoryTreeArray ();
ps_product_category::sortCategoryTreeArray ($arr);
echo category_tree (0, $arr);
?>