Divide a Magento category tree in X columns
I need to divide a Magento category tree into 4 (the number shouldn't be
important) columns that have to have an equal number of items. I have
worked for DAYS trying to find a solution, but I have come to a dead end.
Now, here's what method I have at the moment:
<?php
class My_Class extends Mage_Catalog_Block_Seo_Sitemap_Tree_Category
{
private $total;
private $per_column;
private $collection;
private $counter = 1;
private $split_points;
const UL_COLUMNS = 4;
public function _construct()
{
$this->total = $this->getCollectionSize();
$this->per_column = ceil($this->total / self::UL_COLUMNS);
$this->split_points = array($this->per_column,
(2*$this->per_column), (3*$this->per_column));
}
public function getTreeCategories($parentId, $isChild, $increment =
false)
{
$collection = Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('is_active','1')
->addAttributeToFilter('include_in_menu','1')
->addAttributeToFilter('parent_id',array('eq' =>
$parentId))
->addAttributeToFilter('level', array('gteq' => 2))
->addAttributeToFilter('level', array('lt' => 4))
->addAttributeToSort('position', 'asc');
$html = '';
if($increment)
$this->counter++;
if(in_array($this->counter, $this->split_points))
{
$html .= '</ul>';
$html .= '<ul class="sitemap">';
} else {
$html .= '<ul class="sitemap">';
}
foreach($collection as $category)
{
$html .= '<li style="padding-left: ' .$category->getLevel() .
'0px;">'.$category->getName();
$subcats = $category->getChildren();
if($subcats !== ''){
$html .= $this->getTreeCategories($category->getId(),
true, true);
}
$html .= '</li>';
$this->counter++;
}
$html .= '</ul>';
return $html;
}
I know this is now working. I have tried many ways, but it doesn't work.
The important fact here is that I also need to go 3-4 levels from the main
categories down to its children and put them in the list.
I hope someone may be able to help me.
Thanks!
No comments:
Post a Comment