1

I added my code to my custom header phtml:

<?php
                              $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                              $catId = 2; 
                              $subCategory = $objectManager->create('Magento\Catalog\Model\Category')->load($catId);
                              $subCats = $subCategory->getChildrenCategories();
                              $_helper = $this->helper('Magento\Catalog\Helper\Output');
                              $hasSubcategories = count($subCats) > 0;
                           ?>

                           <?php 
                           
                           foreach ($subCats as $subcat) {
                              $_category = $objectManager->create('Magento\Catalog\Model\Category')->load($subcat->getId());
                              $subcaturl = $subcat->getUrl();
                              ?>
                             
                              
                              <li class="level1 dontsplit parent">
                                       <a class="level1 has-children" href="<?php echo $subcaturl . '?cat=' . $_category->getId() ?>"><?php echo $subcat->getName(); ?> (<?php echo $product_count = $_category->getProductCount();?>)</a>
                                       <?php
                                          $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                                          $object_managertwo = $objectManager->create('Magento\Catalog\Model\Category')->load($subcat->getId());
                                          $subcatslevelthird = $object_managertwo->getChildrenCategories();
                                       ?>
                                          <?php if ($subcatslevelthird->count() > 0) { ?>
                                             <ul class="level1">
                                                <?php
                                                   foreach ($subcatslevelthird as $subcatthird) {
                                                      $_outputhelper = $this->helper('Magento\Catalog\Helper\Output');
                                                      $subcaturl = $subcatthird->getUrl();
                                                ?>
                                                   
                                                   <li class="level2 dontsplit">
                                                      <a class="level2" href="<?php echo $subcaturl ?>"><?php echo $subcatthird->getName(); ?> (<?php echo $subcatthird = $_category->getProductCount();?>)</a>   
                                                   </li>
                                                   
                                                <?php } ?>
                                             </ul>
                                    <?php } ?>
                              </li>
                        <?php } ?>

all working properly except adding class to li element, i need display class parent - only for if menu has submenu, whats wrong?

1 Answer 1

1

You can try this code:

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$catId = 2;
$subCategory = $objectManager->create('Magento\Catalog\Model\Category')->load($catId);
$subCats = $subCategory->getChildrenCategories();
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$hasSubcategories = count($subCats) > 0;
?>

<?php

foreach ($subCats as $subcat) {
$_category = $objectManager->create('Magento\Catalog\Model\Category')->load($subcat->getId());
$subcaturl = $subcat->getUrl();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$object_managertwo = $objectManager->create('Magento\Catalog\Model\Category')->load($subcat->getId());
$subcatslevelthird = $object_managertwo->getChildrenCategories();
$parentClass = "";
if($subcatslevelthird->count() > 0){
    $parentClass = "parent";
}
?>


<li class="level1 dontsplit <?php echo $parentClass; ?>">
    <a class="level1 has-children" href="<?php echo $subcaturl . '?cat=' . $_category->getId() ?>"><?php echo $subcat->getName(); ?> (<?php echo $product_count = $_category->getProductCount();?>)</a>
    <?php if ($subcatslevelthird->count() > 0) { ?>
    <ul class="level1">
        <?php
        foreach ($subcatslevelthird as $subcatthird) {
        $_outputhelper = $this->helper('Magento\Catalog\Helper\Output');
        $subcaturl = $subcatthird->getUrl();
        ?>

        <li class="level2 dontsplit">
            <a class="level2" href="<?php echo $subcaturl ?>"><?php echo $subcatthird->getName(); ?> (<?php echo $subcatthird = $_category->getProductCount();?>)</a>
        </li>

        <?php } ?>
    </ul>
    <?php } ?>
</li>
<?php } ?>

I have loaded sub categories first before adding 1st level li then check if count of child categories available, then added the class parent

Hope this will help you

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.