0

I've got the following piece of code

  $_helper = Mage::registry('current_category');
  $_categoryUrl = $_helper->getUrlKey();
  $_categoryId = $_helper->getId();
  $_subcategories = $_helper->getChildrenCategories();
  echo '<script>';
  echo 'var el = $(document.getElementById("'.$_categoryId.'"));';
  echo 'el.classList.add("active");';
  echo '</script>';

  $_categories = Mage::helper('catalog/category')->getStoreCategories();
  foreach($_categories as $_category) {
  $_subcategories = $_category->getChildrenCategories();
  foreach($_subcategories as $_subcategory){
   echo '<script>';
   echo 'var el = $(document.getElementById("'.$_subcategory->getParentCategory()->getId().'"));';
   echo 'el.classList.add("active");';
   echo '</script>';
  }
 }

What it does is to look for categories and their subcategories, in order to apply a script. The result is that the script -in the first part- is applied to the current category without checking if this is is a parent category or a child category. In the second part, the script gets applied to all subcategories which is not want I'd want.

I just want that Magento identifies the current category and then make a check:

  • If the current category is a parent category, then the script should apply the script only to it, adding the class 'active' to the element of that category.

  • If the current category is a child category, then the script should be applied only to that subcategory, adding the class 'active' to the element of its parent category.

Code so far (v2):

<?php
        /** @var \Mage_Catalog_Model_Category $category */
    $category = Mage::registry('current_category');
    $isChild = $category->getLevel() > 2;
    $isParent = (bool)$category->getChildrenCategories()->getSize();

    $categoryId = null;
    if($isChild) {
        $categoryId = $category->getParentCategory()->getId();
    } elseif($isParent) {
        $categoryId = $category->getId();
    }
?>

<?php if($categoryId): ?>
<script>
<?php
   echo 'if (( “#dropdown-menu” ).is( “.active” )) { ';
   echo 'var el = document.getElementById("'.$categoryId.'");';
   echo 'el.classList.add("active");';
   echo '}';
?>
</script>
<?php endif; ?>

The javascript does not seem to work, it is not to see in the browser's console when inspection the category page.

1 Answer 1

0

Here is a solution that does exactly what you ask but without the inefficient looping and nested foreach's. The only thing I am not sure of how you would want handled is, what is the category is both a parent AND a child.

<?php
    /** @var \Mage_Catalog_Model_Category $category */
    $category = Mage::registry('current_category');
    $isChild = $category->getLevel() > 2;
    $isParent = (bool)$category->getChildrenCategories()->getSize();

    $categoryId = null;
    if ($isChild) {
        $categoryId = $category->getParentCategory()->getId();
    } elseif ($isParent) {
        $categoryId = $category->getId();
    }
?>
<?php if ($categoryId) : ?>
    <script>
        var el = $(document.getElementById('<?php echo $categoryId; ?>'));
        el.classList.add("active");
    </script>
<?php endif; ?>

I should also mention, this logic all belongs inside of a block class. However, I made the answer to best suit what I think you are doing, which is putting it inside of a phtml file.

1
  • Hi Shawn. Thanks for your help. I have tried to implement a javascript for adding a functionality for a hamburger menu. When the hamburger menu gets the class 'active' it shows up, and when one element of the hamburger menu with the ID -that is the same of the category ID- gets the class 'active', it would fold out too. The php logic seems to be allright, but the javascript does not seem to be triggered. Do you have any tips on that? May 7, 2018 at 10:58

Your Answer

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

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