0

I use the following code to get the categories in the left side of my store :

function categoryLoop($id, $is_sub = false){
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$categories = $objectManager->create('Magento\Catalog\Model\Category')->load($id);
if($categories->hasChildren()){
echo '<ul class="leftcat"' . (($is_sub) ? ' class="category_children category-'.$categories->getId().'"' : '') . '>';
        $subcategories = explode(',', $categories->getChildren());
        foreach ($subcategories as $category) {
            $subcategory = $objectManager->create('Magento\Catalog\Model\Category')->load($category);
              echo '<li><a href="'.$subcategory->getUrl().'">';
            echo $subcategory->getName();
            echo "</a></li>";
            if($subcategory->hasChildren()){ categoryLoop($category, true); }
        }
    echo "</ul>";
    }
}
categoryLoop(3); 

How I can add a class "active" to the current / selected category?

Thank you

1 Answer 1

1

You can use Registry to get current Category ID:

$registry = $objectManager->get('Magento\Framework\Registry');

$category = $registry->registry('current_category');
$catId = 0;
if($category) {
   $catId = $category->getId(); 
}

Now by comparing Category ID you can add active class.

Updated Code:

function categoryLoop($id, $currentCatId, $is_sub = false){

   $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
   $categories = $objectManager->create('Magento\Catalog\Model\Category')->load($id);

   if($categories->hasChildren()){
       echo '<ul class="leftcat"' . (($is_sub) ? ' class="category_children category-'.$categories->getId().'"' : '') . '>';

       $subcategories = explode(',', $categories->getChildren());
        foreach ($subcategories as $category) {
            $subcategory = $objectManager->create('Magento\Catalog\Model\Category')->load($category);
            $class = ($currentCatId == $subcategory->getId())?'active':'';
            echo '<li class="'.$class.'"><a href="'.$subcategory->getUrl().'">';
            echo $subcategory->getName();
            echo "</a></li>";
            if($subcategory->hasChildren()){ categoryLoop($category, true); }
        }
      echo "</ul>";
    }
}

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$registry = $objectManager->get('Magento\Framework\Registry');

$category = $registry->registry('current_category');
$currentCatId = 0;
if($category) {
   $currentCatId = $category->getId(); 
}
categoryLoop(3, $currentCatId); 
7
  • I have a big request can you add this part of code in my code please? I am not so good. Thanks
    – Robert
    Commented Jun 20, 2018 at 11:41
  • Please check I have added updated code Commented Jun 20, 2018 at 11:47
  • thank you so much I have this error: Undefined variable: objectManager in ...
    – Robert
    Commented Jun 20, 2018 at 12:21
  • please check now Commented Jun 20, 2018 at 12:23
  • 1
    is perfect, thank you very much or your help.
    – Robert
    Commented Jun 20, 2018 at 13:09

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.