cancel
Showing results for 
Search instead for 
Did you mean: 

Category navigation

Category navigation

Hi,

 

I have been modifying the blank theme to setting it up according to my theme that I am building.

 

Now I need the category navigation to work like this:

  • if I click on a category - it will display the sub categories

my problem is that if there is no sub-categories in a particular category, it will not open the page with the products like a sub-category does.

 

is there a way to set the navigation as, if there are sub-categories, on click will show the sub categories menu, if there are no sub-categories, it will open the products page?

 

this is the code that I have right now:

<?php
    $categories = $this->getStoreCategories(true,false,true);
    $categoryHelper = $this->getCategoryHelper();
?>
<div class="bb-products" id='products'>
    <div class="bb-title">
        Product Categories
    </div> 
    <?php $col=1; ?>
    <div class="categoryList">
        <ul>
            <?php
                foreach($categories as $category):
                if (!$category->getIsActive())
                {
                    continue;
                }
                $categoryName = preg_replace('#[ -]+#', '-', $category->getName());
            ?>

        <li onclick="showSubCategory('<?php echo $categoryName; ?>')" id="<?php echo $categoryName; ?>" class="bb-category categoryColor<?php echo $col ?>"><?php echo $category->getName(); ?></li>
        <?php  
                    if($childrenCategories = $this->getChildCategories($category)):
                        ?>
                    <div id='parent_<?php echo $categoryName ?>' class='parent'>
                    <?php
                         foreach($childrenCategories as $childrenCategory){ 
            ?>       
                    <div class="categoriesTable chidle" id='sub_<?php echo $categoryName ?>'>
            <?php
                            break;
                         }

                    ?>
                   
                    <?php
                    foreach($childrenCategories as $childrenCategory):
                         if (!$childrenCategory->getIsActive()) {
                            continue;
                         }
                    if ( $this->hasChildren($childrenCategory->getId())==0){
                        ?>
                   <a href="<?php echo $categoryHelper->getCategoryUrl($childrenCategory) ?>"> 
                     <div class="categoriesElement productBox<?php echo $col; ?>">
                        <?php
                    } else{
                         $subCategoryName = preg_replace('#[ -]+#', '-', $childrenCategory->getName());
                    ?>
                        <div class="categoriesElement productBox<?php echo $col; ?>" onclick="showSubSubCategories('<?php echo  $subCategoryName ?>', '<?php echo $categoryName ?>');" >
                    <?php
                    }
                    ?>
                        <div class="categoriesElementPicture">
                            <?php 
                            if ( $this->hasChildren($childrenCategory->getId())>0){
                                ?>
                                <div class="plus">[+]</div>
                <?php
                                }
                                $catId = $childrenCategory->getId();
                                $categoryAgain = $this->getCategoryView()->load($catId);
                                $_outputhelper = $this->helper('Magento\Catalog\Helper\Output');
                                $_imgHtml   = '';
                                if ($_imgUrl = $categoryAgain->getImageUrl()) {
                                    $_imgHtml = '<img src="' . $_imgUrl . '" />';
                                    $_imgHtml = $_outputhelper->categoryAttribute($categoryAgain, $_imgHtml, 'image');
                                    /* @escapeNotVerified */ 
                                    echo $_imgHtml;
                                }

                             ?>
                        </div>
                        <div class="categoriesElementName">
                            <?php echo $childrenCategory->getName();  ?>
                        </div>
                    </div>
                    <?php
                      if ( $this->hasChildren($childrenCategory->getId())==0){
                        ?>
                        </a> 
                <?php
                        }
                    endforeach;

                         foreach($childrenCategories as $childrenCategory){
            ?>       
                    </div>
            <?php
                            break;
                         }
                    endif;

        if($childrenCategories = $this->getChildCategories($category)):
        foreach($childrenCategories as $childrenCategory):
             if (!$childrenCategory->getIsActive()) {
                continue;
             }
        if($childrenSubCategories = $this->getChildCategories($childrenCategory)):
             $subCategoryName = preg_replace('#[ -]+#', '-', $childrenCategory->getName());
            ?>
            <div class="subChild" id="sub_sub_<?php echo  $subCategoryName; ?>">
        <?php
               foreach ($childrenSubCategories as $childrenSubCategory):
                ?>
           <a href="<?php echo $categoryHelper->getCategoryUrl($childrenSubCategory) ?>"> 
             <div class="categoriesElement productBox<?php echo $col; ?>">
                <div class="categoriesElementPicture">
                    <?php 

                        $catId = $childrenSubCategory->getId();
                        $categoryAgain = $this->getCategoryView()->load($catId);
                        $_outputhelper = $this->helper('Magento\Catalog\Helper\Output');
                        $_imgHtml   = '';
                        if ($_imgUrl = $categoryAgain->getImageUrl()) {
                            $_imgHtml = '<img src="' . $_imgUrl . '" />';
                            $_imgHtml = $_outputhelper->categoryAttribute($categoryAgain, $_imgHtml, 'image');
                            /* @escapeNotVerified */ 
                            echo $_imgHtml;
                        }

                     ?>
                </div>
                <div class="categoriesElementName">
                    <?php echo $childrenSubCategory->getName();  ?>
                </div>
            </div>
            </a> 
        <?php
            endforeach;
        ?>
            </div>
            <?php
            endif;
            endforeach;
        endif;

?>
    </div>
<?php
        $col++;
        if ($col>6) {
            $col=1;
        }
        endforeach;
        ?>
        </ul>
    </div>
</div>