Show and count categories in OpenCart - php

I have this issue with OpenCart where I want to display my shop categories in a custom way and count the parent categories.
I currently modified the code so far that I get the following output
<ul id="catOpContainer">
<li id="switchCatOp1">Parent Cat 1
<ul id="catOp1">
<li>Child cat 1</li>
Child Cat 2</li>
</ul>
</li>
Parent Cat 2
<ul id="catOp1">
<li>Child cat 1</li>
Child cat 2</li>
</ul>
</li>
Parent Cat 3</li>
</ul>
</ul>
instead of the desired
<ul id="catOpContainer">
<li id="switchCatOp1">Parent Cat 1
<ul id="catOp1">
<li>Child Cat 1</li>
<li>Child Cat 2</li>
</ul>
</li>
<li id="switchCatOp2">Parent Cat 2
<ul id="catOp2">
<li>Child Cat 1</li>
<li>Child Cat 2</li>
<li>Child Cat 3</li>
</ul>
</li>
</ul>
It's obvious that there are some missing elements, but I have no clue about a possible solution. I also don't have a clue how to count the parent categories, so that I can toggle the subcategories.
I currently have the following code snippet:
<?php
class ControllerModuleCategory extends Controller {
protected $category_id = 0;
protected $path = array();
protected function index() {
$this->language->load('module/category');
$this->data['heading_title'] = $this->language->get('heading_title');
$this->load->model('catalog/category');
$this->load->model('tool/seo_url');
if (isset($this->request->get['path'])) {
$this->path = explode('_', $this->request->get['path']);
$this->category_id = end($this->path);
}
$this->data['category'] = $this->getCategories(0);
$this->id = 'category';
if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/category.tpl')) {
$this->template = $this->config->get('config_template') . '/template/module/category.tpl';
} else {
$this->template = 'default/template/module/category.tpl';
}
$this->render();
}
protected function getCategories($parent_id, $current_path = '') {
$category_id = array_shift($this->path);
$output = '';
$results = $this->model_catalog_category->getCategories($parent_id);
if ($results) {
if ($parent_id == 0) {
$output .= '<li id="switchCatOp1">';
} else {
$output .= '<ul id="catOp1"><li>';
}
}
foreach ($results as $result) {
if (!$current_path) {
$new_path = $result['category_id'];
} else {
$new_path = $current_path . '_' . $result['category_id'];
}
$output .= '';
$children = '';
// if ($category_id == $result['category_id']) {
$children = $this->getCategories($result['category_id'], $new_path);
// }
if ($this->category_id == $result['category_id']) {
$output .= '<a href="' . $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&path=' . $new_path) . '">' . $result['name'] . '</a>';
} else {
$output .= '<a href="' . $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&path=' . $new_path) . '">' . $result['name'] . '</a>';
}
$output .= $children;
$output .= '</li>';
}
if ($results) {
$output .= '</ul>';
}
return $output;
}
}
?>
I really hope someone knows a solution.

You do not need to customize controller. It has already Parent and child category listing, Just open the category module, and past below code.
<div class="box-category">
<ul>
<?php foreach ($categories as $category) { ?>
<li>
<?php if ($category['category_id'] == $category_id) { ?>
<?php echo $category['name']; ?>
<?php } else { ?>
<?php echo $category['name']; ?>
<?php } ?>
<?php if ($category['children']) { ?>
<ul>
<?php foreach ($category['children'] as $child) { ?>
<li>
<?php if ($child['category_id'] == $child_id) { ?>
<?php echo $child['name']; ?>
<?php } else { ?>
<?php echo $child['name']; ?>
<?php } ?>
</li>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } ?>
</ul>
</div>

Related

How to display magento product categories in column wise

I'm trying to display all the categories below the main title, so I want to display four categories in each column. The below is my code
<?php
$_category = $this->getCurrentCategory();
$collection = Mage::getModel('catalog/category')->getCategories($_category->entity_id);
$helper = Mage::helper('catalog/category');
?>
<ul class="pull-left">
<?php foreach ($collection as $cat):?>
<li>
<a style="color:#fff;" href="<?php echo $helper->getCategoryUrl($cat);?>">
<cite><?php echo $cat->getName();?><?php
?></cite>
</a>
</li>
<?php endforeach;?>
</ul>
HTML Output
<ul class="pull-left">
<li>....</li>
<li>....</li>
<li>....</li>
<li>....</li>
<li>....</li>
<li>....</li>
<li>....</li>
<li>....</li>
.
.
.
.
</ul>
I want to define four li items in every ul. Like this
<ul class="pull-left">
<li>...</li>
<li>...</li>
<li>...</li>
<li>...</li>
</ul>
<ul class="pull-left">
<li>...</li>
<li>...</li>
<li>...</li>
<li>...</li>
</ul>
I also want to display product count and I've tried this
<?php
$products_count = Mage::getModel('catalog/category')->load($categoryId)->getProductCount();
?>
but it is not working.
I am using this code on my category page and it is giving me count.
Please have a look at the code I have added before tag.
<?php
$loadCategory = Mage::registry('current_category');
//echo 'loadCategory = '.$_categoryID = $loadCategory->getId();
$subCategories = explode(',', $loadCategory->getChildren());
?>
<ul class="pull-left">
<?php $i=1;
foreach ( $subCategories as $subCategoryId )
{
$cat = Mage::getModel('catalog/category')->load($subCategoryId);
if($cat->getIsActive())
{
if($cat->getImageUrl())
{
//echo ''.$cat->getName().'</br>';
echo '<li>';
echo '<a style="color:#fff;" href="'.$cat->getUrl().'">'.$this->escapeHtml($cat->getName()); echo"<br>";
echo $total = Mage::getModel('catalog/layer')->setCurrentCategory($subCategoryId)->getProductCollection()->getSize();
echo'</a>';
echo '</li>';
if($i%4==0)
{
echo'</ul><ul class="pull-left">';
}
}
}
$i++;
}
?>
Please use this code. This will surely work for you.
<?php
$category = Mage::getModel('catalog/category')->load($categoryID);
echo $total = Mage::getModel('catalog/layer')->setCurrentCategory($category)->getProductCollection()->getSize();
?>
Thanks,
Please use this one.
You can adjust ul ,li as per wish. Right now this will list four subcategories in a single row.
This code will give you the subcategories of a parent category.
<?php
$loadCategory = Mage::registry('current_category');
//echo 'loadCategory = '.$_categoryID = $loadCategory->getId();
$subCategories = explode(',', $loadCategory->getChildren());
?>
<ul class="pull-left">
<?php $i=1;
foreach ( $subCategories as $subCategoryId )
{
$cat = Mage::getModel('catalog/category')->load($subCategoryId);
if($cat->getIsActive())
{
if($cat->getImageUrl())
{
//echo ''.$cat->getName().'</br>';
echo '<li>';
echo '<a style="color:#fff;" href="'.$cat->getUrl().'">'.$this->escapeHtml($cat->getName()).'</a>';
echo '</li>';
if($i%4==0)
{
echo'</ul><ul class="pull-left">';
}
}
}
$i++;
}
?>
</ul>

Drupal 7 bootstrap dropdown menu

I'm a newbie of Drupal. I'm trying to develop a bootstrap 3 template, but I have a problem with the navbar dropdown menu implementation. I followed these steps:
in my mytheme/templates folder I create a page.tpl.php file with the following code:
if ($page['header'])
...
$main_menu = variable_get('menu_main_links_source', 'main-menu');
$tree = menu_tree($main_menu);
print drupal_render($tree);
...
in mytheme folder I create a template.php file with these functions:
function mytheme_menu_tree($variables) {
return '<ul class="nav navbar-nav">' . $variables['tree'] . '</ul>';
}
function mytheme_menu_link(array $variables) {
$element = $variables['element'];
$sub_menu = '';
$dropdown = '';
if ($element['#below']) {
$sub_menu = drupal_render($element['#below']);
$dropdown = 'class="dropdown"';
$element['#localized_options']['attributes']['class'][] = 'dropdown-toggle';
}
$output = l($element['#title'], $element['#href'], $element['#localized_options']);
return '<li ' .$dropdown. ' >' . $output . $sub_menu . "</li>\n";
}
with this code I'm at a good point, but I need to remove classes "nav navbar-nav" from children and add the class "dropdown-menu".
This is the result of my code:
<ul class="nav navbar-nav">
<li>XYZ</li>
<li>ASD</li>
<li class="dropdown">XXX
<ul class="nav navbar-nav">
<li>...</li>
<li>...</li>
<li>...</li>
</ul>
</li>
</ul>
and this is what I would like to obtain:
<ul class="nav navbar-nav">
<li>XYZ</li>
<li>ASD</li>
<li class="dropdown">XXX
<ul class="dropdown-menu"> <!-- HERE IS THE DIFFERENCE -->
<li>...</li>
<li>...</li>
<li>...</li>
</ul>
</li>
</ul>
Maybe I can do something like this:
function mytheme_menu_tree($variables) {
if ( [check if I'm at the first level] ) {
return '<ul class="nav navbar-nav">' . $variables['tree'] . '</ul>';
} else {
return '<ul class="dropdown-menu">' . $variables['tree'] . '</ul>';
}
}
but I don't know how... Any idea?
I too am a bit of a newbie when it comes to Drupal and was also having this problem. I have tweaked your function and it works for me:
function SeatradeKorea_menu_link(array $variables) {
$element = $variables['element'];
$sub_menu = '';
$dropdown = '';
if ($element['#below']) {
$sub_menu = drupal_render($element['#below']);
$sub_menu = str_replace('nav navbar-nav', 'dropdown-menu', $sub_menu);
$dropdown = 'class="dropdown"';
$element['#localized_options']['attributes']['class'][] = 'dropdown-toggle';
}
$output = l($element['#title'], $element['#href'], $element['#localized_options']);
return '<li ' .$dropdown. ' >' . $output . $sub_menu . "</li>\n";
}
I have added a str_replace() into your #below element to replace the "nav navbar-nav" with "dropdown-menu"

Tree Structure using ul,li format in php

Code for Tree structure using ul, li in php...
*In the output i am missing ul or in some place.*
I want this for checkboxes when we click the parent check box their childs must be selected and sub childs too.
<?php
$groups = array(
array('depth'=>0,'value'=>'DEF'),
array('depth'=>1,'value'=>'DEF 1'),
array('depth'=>2,'value'=>'DEF_1'),
array('depth'=>1,'value'=>'DEF2'),
array('depth'=>0,'value'=>'GROUP'),
array('depth'=>1,'value'=>'GROUP1'),
array('depth'=>1,'value'=>'GROUP2'),
array('depth'=>2,'value'=>'GROUP2_1'),
array('depth'=>2,'value'=>'TELUGU'),
array('depth'=>3,'value'=>'RAM'),
array('depth'=>0,'value'=>'GROUP3'),
array('depth'=>1,'value'=>'GROUP3_1'),
array('depth'=>1,'value'=>'GROUP3_2'),
array('depth'=>1,'value'=>'GROUP3_3'),
array('depth'=>1,'value'=>'DEF2_1'),
array('depth'=>0,'value'=>'GROUP4'),
array('depth'=>0,'value'=>'T8')
);
$count = count($groups);
$old_depth = 0;
echo '<ul>'.chr(13).chr(10);
for($i = 0; $i<$count; $i++)
{
if($old_depth==$groups[$i]['depth'])
{
echo '<li>';
echo $groups[$i]['value'];
if(isset($groups[$i+1]))
{
if($old_depth!=$groups[$i+1]['depth']){
echo '<ul>';
}
else
{
echo '</li>';
}
}
}
if($old_depth<$groups[$i]['depth'])
{
echo '<li>';
echo $groups[$i]['value'];
if(isset($groups[$i+1]))
{
if(($groups[$i]['depth'])+1==$groups[$i+1]['depth']){
echo '<ul>';
}
else
{
echo '</li>';
if(!($groups[$i]['depth']==$groups[$i+1]['depth']))
{
echo '</ul></li>';
}
if($groups[$i+1]['depth']==0)
{
echo '</ul></li>';
}
}
}
else
{
echo '</ul>';
}
}
}
echo '</li></ul>';
?>
I want the output in this format.
<ul>
<li>DEF
<ul>
<li>DEF1
<ul>
<li>DEF_1</li>
</ul>
</li>
<li>DEF2</li>
</ul>
</li>
<li>GROUP
<ul>
<li>GROUP1</li>
<li>GROUP2
<ul>
<li>GROUP2_1</li>
<li>TELUGU
<ul>
<li>RAM</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>GROUP3
<ul>
<li>GROUP3_1</li>
<li>GROUP3_2</li>
<li>GROUP3_3</li>
<li>DEF2_1</li>
</ul>
</li>
<li>GROUP4</li>
<li>T8</li>
</ul>

Magento retrieve children from specific category

I have a problem with closing my li's and ul's at the correct moment.
With the code we have we retrieve all childeren of a specific categorie in the magento shop.
Now the problem is that i want to divide all children in lists. So we can have them sorted by category -> sub-category -> sub-sub category. I want my structure to be;
<ul>
<li>
<a>Head Child</a>
<ul>
<li>
<a>Sub child</a>
<ul>
<li><a>Sub sub child</a></li>
<li><a>Sub sub child</a></li>
</ul>
</li>
</ul>
</li>
<li>
<a>Head Child</a>
<ul>
<li>
<a>Sub child</a>
<ul>
<li><a>Sub sub child</a></li>
<li><a>Sub sub child</a></li>
</ul>
</li>
</ul>
</li>
</ul>
The output im getting now is
<ul>
<a title="View the products for the " href="#">Head child</a>
<li class="sub_cat">
<a title="View the products for the " href="#">Sub child</a>
</li>
<li class="sub_cat">
<a title="View the products for the " href="#">Sub sub child</a>
</li>
</ul>
This is our php code;
<?php
$cat = Mage::getModel('catalog/category')->load(9);
$subcats = $cat->getChildren();
foreach(explode(',',$subcats) as $subCatid)
{
$_category = Mage::getModel('catalog/category')->load($subCatid);
if($_category->getIsActive()) {
echo '<ul>'.$_category->getName().'';
$sub_cat = Mage::getModel('catalog/category')->load($_category->getId());
$sub_subcats = $sub_cat->getChildren();
foreach(explode(',',$sub_subcats) as $sub_subCatid)
{
$_sub_category = Mage::getModel('catalog/category')->load($sub_subCatid);
if($_sub_category->getIsActive()) {
echo '<li class="sub_cat">'.$_sub_category->getName().'';
$sub_sub_cat = Mage::getModel('catalog/category')->load($sub_subCatid);
$sub_sub_subcats = $sub_sub_cat->getChildren();
foreach(explode(',',$sub_sub_subcats) as $sub_sub_subCatid)
{
$_sub_sub_category = Mage::getModel('catalog/category')->load($sub_sub_subCatid);
if($_sub_sub_category->getIsActive()) {
echo '<li class="sub_cat">'.$_sub_sub_category->getName().'';
}
}
}
}
echo '</ul>';
}
}
?>
You should consider using recursive function here
$cat = Mage::getModel('catalog/category')->load(9);
$html = '';
$html = getSubCategoriesHTML($cat, $html);
function getSubCategoriesHTML($cat, $html) {
$html .= ''.$cat->getName().'';
$html .= '<ul>';
$subcats = $cat->getChildren();
foreach(explode(',',$subcats) as $subCatid) {
$_category = Mage::getModel('catalog/category')->load($subCatid);
if($_category->getIsActive()) {
$html .= '<li>';
$html .= getSubCategoriesHTML($_category, $html);
$html .= '</li>;
}
}
$html .= '</ul>';
return $html;
}
You can add this function in a helper or in your category model.
I haven't tested it at all so it may not work correctly. But I hope it can help you.

How do I add different class for my ordered lists

I want add left, center & right class to my ordered lists while loop.
Code
<?php while ($fetch) { ?>
<li>haha</li>
<?php } ?>
Results should be
<ul>
<li class="left">haha</li>
<li class="center">haha</li>
<li class="right">haha</li>
<li class="left">haha</li>
<li class="center">haha</li>
<li class="right">haha</li>
</ul>
Let me know
<?php $classes = array("left","center","right");
$i = 0;
while ($fetch) {
?>
<li class="<?php echo $classes[$i++ % 3] ?>">haha</li>
<?php } ?>
$cnt=0;
while ($fetch)
{
switch ($cnt%3)
{
case 0 : $class = 'left'; break;
case 1 : $class = 'center'; break;
case 2 : $class = 'right'; break;
}
echo '<li class="', $class, '">haha</li>';
++$cnt;
}
I've just tested the following code and verified that it produces the desired output:
<?php
$items = array('haha', 'haha', 'haha', 'haha', 'haha', 'haha');
$cssClasses = array('left', 'center', 'right');
echo "<ul>\n";
$i=0;
foreach ($items as $item) {
echo "\t<li class=\"" . $cssClasses[$i++ % 3] . '">' . $item . "</li>\n";
}
echo "</ul>\n";
?>
The output is:
<ul>
<li class="left">haha</li>
<li class="center">haha</li>
<li class="right">haha</li>
<li class="left">haha</li>
<li class="center">haha</li>
<li class="right">haha</li>
</ul>

Categories