我创建了一个代码来按类别过滤产品集合。使用以下代码创建自己的左侧块。
我有完美的过滤工作,我只是很难渲染显示器。
2级类别工作正常,只显示
选择后,它会显示子类别及其子类别。
我需要它像第一级那样分解。什么是最好的方法?
<?php
$root_category_id = Mage::app()->getStore()->getRootCategoryId();
$filterCategoryId = Mage::app()->getRequest()->getParam('cat');
$products = Mage::getSingleton('catalogsearch/advanced')->getProductCollection();
$catIds = array();
if (isset($products)) foreach ($products as $product) {
if ($product->isSaleable()) {
$ids = $product->getCategoryIds();
foreach ($ids as $id) $catIds[$id] = 1;
}
}
if (!isset($catIds)) return false;
$categories = array();
// Filters rest of categories
if ($filterCategoryId) {
$filterCategory = Mage::getModel('catalog/category')->load($filterCategoryId);
$filterChildren = $filterCategory->getAllChildren(true);
foreach ($catIds as $id => $x) {
$category = Mage::getModel('catalog/category')->load($id);
if (in_array($root_category_id, $category->getParentIds()) && in_array($id, $filterChildren) && $id != $filterCategoryId) {
$categories[$id] = $category->getName();
} else {
unset($catIds[$id]);
}
}
} else {
// Filters the first level categoires
foreach ($catIds as $id => $x) {
$category = Mage::getModel('catalog/category')->load($id);
if (in_array($root_category_id, $category->getParentIds()) && $category->getLevel() == 2) {
$categories[$id] = $category->getName();
} else {
unset($catIds[$id]);
}
}
}
if(isset($categories) && sizeof($categories) > 0):
$url = Mage::app()->getRequest()->getRequesturi();
$url = Mage::helper("core/url")->removeRequestParam($url, 'cat');
?>
<div class="page-subtitle" style="margin:6px auto 14px 6px;"><h2>Shop By Category</h2></div>
<ul class="brandsnav">
<?php foreach($categories as $id=>$name): ?>
<li class="cf">
<span class="subnav_trigger"></span>
<a href="<?php
echo Mage::helper("core/url")->addRequestParam($url, array("cat"=>$id)); ?>"><?php echo $name;?></a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
答案 0 :(得分:0)
我只需要为foreach语句添加条件。搞定了。