3

I have a problem on my product categories page the navigation that we have going down the left side displays all the categories we need it to and also it bold a subcategory when you are on it, However when you are not on any sub categories and you are on the main categories page The bottom category is always in bold even when you are not on it.

This is my code which creates the navigation (this is a .phtml file)

<?php
ob_start();

$store_categories = $this->getLeftCategories();
if ($count = count($store_categories)) {

$block_title = $this->getBlockTitle();
if ( empty($block_title) ) $block_title = 'Categories';
?>
<div class="block block-left-nav">
    <div class="block-title">
        <strong><span><?php echo $this->__($block_title) ?></span></strong>
    </div>
    <div class="block-content">
        <ul id="left-nav">
        <?php
        $i=0;
        foreach ($store_categories as $_category) {
            $class = array();
            if (!$i) {
                $class[] = 'first';
            } elseif ($i == $count) {
                $class[] = 'last';
            }
            echo $this->drawOpenCategoryItem($_category, 0, $class);
            $i++;
        } ?>
        </ul>
    </div>
</div>
<?php
} 
// if ($count = count($store_categories)) {
$mdms_blocks = Mage::registry('mdms_blocks');
if ( !$mdms_blocks ) {
    $mdms_blocks = array();
} else {
    Mage::unregister('mdms_blocks');
}
$mdms_blocks['block_left_nav'] = ob_get_clean();
Mage::register('mdms_blocks', $mdms_blocks);

I assume that what opens the categories is the drawOpenCategoryItem function

This is the code in the navigation.php which has that function in it but i cant see anything wrong.

<?php
/**
 * @version   1.0 12.0.2012
 * @author 
 * @copyright Copyright (C) 2010 - 2012 
 */

class our_ThemeSettings_Block_Navigation extends Mage_Catalog_Block_Navigation
{

    /**
     * columns html
     *
     * @var array
     */
    protected $_columnHtml;

    /*
     * @var Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection
     */
    protected $_leftCategories;

    /**
     * top level parent category for current category
     *
     * @var int
     */
    protected $_parent;

    protected function _construct()
    {
        $path = $this->getCurrentCategoryPath();
        $parent = $path[count($path)-2];
        if (!$parent) {
            $parent = Mage::app()->getStore()->getRootCategoryId();
        }
        $this->_parent = $parent;
    }


    /**
     * render category html
     *
     * @param Mage_Catalog_Model_Category $category
     * @param integer $level
     * @param array $levelClass
     * @return string
     */
    public function drawOpenCategoryItem($category, $level = 0, array $levelClass = null)
    {
        $html = array();

        if (!$category->getIsActive()) return '';

        if (!isset($levelClass)) $levelClass = array();
        $combineClasses = array();

        $combineClasses[] = 'level' . $level;
        if ($this->_isCurrentCategory($category)) {
            $combineClasses[] = 'active';
        } else {
            $combineClasses[] = $this->isCategoryActive($category) ? 'parent' : 'inactive';
        }
        $levelClass[] = implode('-', $combineClasses);
        $levelClass = array_merge($levelClass, $combineClasses);
        $levelClass[] = $this->_getClassNameFromCategoryName($category);

        $html[1] = '<a href="' . $this->getCategoryUrl($category) . '">' . $this->escapeHtml($category->getName()) . '</a>' . "\n";

        if ( in_array($category->getId(), $this->getCurrentCategoryPath()) ) {
            $children = $this->_getLeftCategoryCollection()
                    ->addIdFilter($category->getChildren());

            $hasChildren = $children && ($childrenCount = count($children));
            if ($hasChildren) {
                $htmlChildren = '';

                foreach ($children as $i => $child)
                {
                    $class = array();
                    if ($childrenCount == 1) {
                        $class[] = 'only';
                    }
                    else
                    {
                        if (!$i) $class[] = 'first';
                        if ($i == $childrenCount - 1) $class[] = 'last';
                    }
                    $htmlChildren .= $this->drawOpenCategoryItem($child, $level + 1, $class);
                }

                if (!empty($htmlChildren)) {
                    $levelClass[] = 'open';

                    $html[2] = '<ul>' . "\n"
                            . $htmlChildren . "\n"
                            . '</ul>';
                }
            }
        }

        $html[0] = sprintf('<li class="%s">', implode(" ", $levelClass)) . "\n";
        $html[3] = "\n" . '</li>' . "\n";

        ksort($html);
        return implode('', $html);
    }

    /**
     * Convert the category name into a string that can be used as a css class
     *
     * @param Mage_Catalog_Model_Category $category
     * @return string
     */
    protected function _getClassNameFromCategoryName($category)
    {
        $name = $category->getName();
        $name = preg_replace('/-{2,}/', '-', preg_replace('/[^a-z-]/', '-', strtolower($name)));
        while ($name && $name{0} == '-') $name = substr($name, 1);
        while ($name && substr($name, -1) == '-') $name = substr($name, 0, -1);
        return $name;
    }

    /**
     * Check if the current category matches the passed in category
     *
     * @param Mage_Catalog_Model_Category $category
     * @return bool
     */
    protected function _isCurrentCategory($category)
    {
        return ($cat = $this->getCurrentCategory()) && $cat->getId() == $category->getId();
    }

    /**
     * return top level category name
     *
     * @return string
     */
    public function getBlockTitle() {
        if ( $this->_parent == Mage::app()->getStore()->getRootCategoryId() ) {
            return '';
        } else {
            return Mage::getModel('catalog/category')->load($this->_parent)->getName();
        }
    }

    /**
     * Get sibling catagories
     *
     * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection
     */
    public function getLeftCategories()
    {
        $parent = Mage::getModel('catalog/category')->load($this->_parent);
        if ( $parent['is_active'] == 0 || $parent['include_in_menu'] == 0 )
            return null;

        if (!isset($this->_leftCategories)) {
            $this->_leftCategories = $this->_getLeftCategoryCollection()
                ->addIdFilter($parent->getChildren());
        }
        return $this->_leftCategories;
    }

    /**
     * @return Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection
     */
    protected function _getLeftCategoryCollection()
    {
        $collection = Mage::getResourceModel('catalog/category_collection');
        $collection->addAttributeToSelect('url_key')
                ->addAttributeToSelect('name')
                ->addAttributeToSelect('all_children')
                ->addAttributeToFilter('is_active', 1)
                ->addAttributeToFilter('include_in_menu', 1)
                ->setOrder('position', 'ASC')
                ->joinUrlRewrite();
        return $collection;
    }

    /**
     * Render category to html
     *
     * @param Mage_Catalog_Model_Category $category
     * @param int Nesting level number
     * @param boolean Whether ot not this item is last, affects list item class
     * @param boolean Whether ot not this item is first, affects list item class
     * @param boolean Whether ot not this item is outermost, affects list item class
     * @param string Extra class of outermost list items
     * @param string If specified wraps children list in div with this class
     * @param boolean Whether ot not to add on* attributes to list item
     * @return string
     */
    protected function _renderCategoryMenuItemHtml($category, $level = 0, $isLast = false, $isFirst = false,
                                                   $isOutermost = false, $outermostItemClass = '', $childrenWrapClass = '', $noEventAttributes = false)
    {
        if (!$category->getIsActive()) {
            return '';
        }
        $html = array();

        // get all children
        if (Mage::helper('catalog/category_flat')->isEnabled()) {
            $children = (array)$category->getChildrenNodes();
            $childrenCount = count($children);
        } else {
            $children = $category->getChildren();
            $childrenCount = $children->count();
        }
        $hasChildren = ($children && $childrenCount);

        // select active children
        $activeChildren = array();
        foreach ($children as $child) {
            if ($child->getIsActive()) {
                $activeChildren[] = $child;
            }
        }
        $activeChildrenCount = count($activeChildren);
        $hasActiveChildren = ($activeChildrenCount > 0);

        // prepare list item html classes
        $classes = array();
        $classes[] = 'level' . $level;
        $classes[] = 'nav-' . $this->_getItemPosition($level);
        if ($this->isCategoryActive($category)) {
            $classes[] = 'active';
        }
        $linkClass = '';
        if ($isOutermost && $outermostItemClass) {
            $classes[] = $outermostItemClass;
            $linkClass = ' class="' . $outermostItemClass . '"';
        }
        if ($isFirst) {
            $classes[] = 'first';
        }
        if ($isLast) {
            $classes[] = 'last';
        }
        if ($hasActiveChildren) {
            $classes[] = 'parent';
        }

        // prepare list item attributes
        $attributes = array();
        if (count($classes) > 0) {
            $attributes['class'] = implode(' ', $classes);
        }
        if ($hasActiveChildren && !$noEventAttributes) {
            $attributes['onmouseover'] = 'toggleMenu(this,1)';
            $attributes['onmouseout'] = 'toggleMenu(this,0)';
        }

        // assemble list item with attributes
        $htmlLi = '<li';
        foreach ($attributes as $attrName => $attrValue) {
            $htmlLi .= ' ' . $attrName . '="' . str_replace('"', '\"', $attrValue) . '"';
        }
        $htmlLi .= '>';
        $html[] = $htmlLi;

        $html[] = '<a href="' . $this->getCategoryUrl($category) . '"' . $linkClass . '>';
        $html[] = '<span>' . $this->escapeHtml($category->getName()) . '</span>';
        $html[] = '</a>';

        $columnItemsNum = array();
        if ($level == 0 && $activeChildrenCount) {
            $items_per_column = Mage::getStoreConfig('theme/navigation/column_items');
            $columns = ceil($activeChildrenCount / $items_per_column);
            $columnItemsNum = array_fill(0, $columns, $items_per_column);
            $this->_columnHtml = array();
        }

        // render children
        $htmlChildren = '';
        $j = 0; //child index
        $i = 0; //column index
        $itemsCount = $activeChildrenCount;
        if (isset($columnItemsNum[$i])) {
            $itemsCount = $columnItemsNum[$i];
        }
        foreach ($activeChildren as $child) {

            if ($level == 0) {
                $isLast = (($j + 1) == $itemsCount || $j == $activeChildrenCount - 1);
                if ($isLast) {
                    $i++;
                    if (isset($columnItemsNum[$i])) {
                        $itemsCount += $columnItemsNum[$i];
                    }
                }
            } else {
                $isLast = ($j == $activeChildrenCount - 1);
            }

            $childHtml = $this->_renderCategoryMenuItemHtml(
                $child,
                ($level + 1),
                $isLast,
                ($j == 0),
                false,
                $outermostItemClass,
                $childrenWrapClass,
                $noEventAttributes
            );
            if ($level == 0) {
                $this->_columnHtml[] = $childHtml;
            } else {
                $htmlChildren .= $childHtml;
            }
            $j++;
        }

        if ($level == 0 && $this->_columnHtml) {
            $i = 0;
            foreach ($columnItemsNum as $columnNum) {
                $chunk = array_slice($this->_columnHtml, $i, $columnNum);
                $i += $columnNum;
                $htmlChildren .= '<li ' . (count($this->_columnHtml) == $i ? 'class="last"' : '') . '><ol>';
                foreach ($chunk as $item) {
                    $htmlChildren .= $item;
                }
                $htmlChildren .= '</ol></li>';
            }
        }

        if (!empty($htmlChildren)) {
            if ($childrenWrapClass) {
                $html[] = '<div class="' . $childrenWrapClass . '">';
            }
            $html[] = '<ul class="level' . $level . '">';
            $html[] = $htmlChildren;
            $html[] = '</ul>';
            if ($childrenWrapClass) {
                $html[] = '</div>';
            }
        }

        $html[] = '</li>';

        $html = implode("\n", $html);
        return $html;
    }


    /**
     * Render categories menu in selectbox element
     *
     * @param int Level number for list item class to start from
     * @param string Extra class of outermost list items
     * @param string If specified wraps children list in div with this class
     * @return string
     */
    public function renderCategoriesSelectOptions($level = 0, $outermostItemClass = '', $childrenWrapClass = '')
    {
        $activeCategories = array();
        foreach ($this->getStoreCategories() as $child) {
            if ($child->getIsActive()) {
                $activeCategories[] = $child;
            }
        }
        $activeCategoriesCount = count($activeCategories);
        $hasActiveCategoriesCount = ($activeCategoriesCount > 0);

        if (!$hasActiveCategoriesCount) {
            return '';
        }

        $html = '<option value="">' . $this->__('- Please select category -') . '</option>';
        $j = 0;
        foreach ($activeCategories as $category) {
            $html .= $this->_renderCategorySelectOption(
                $category,
                $level,
                ($j == $activeCategoriesCount - 1),
                ($j == 0),
                true,
                $outermostItemClass,
                $childrenWrapClass,
                true
            );
            $j++;
        }

        return $html;
    }


    /**
     * Render category to html
     *
     * @param Mage_Catalog_Model_Category $category
     * @param int Nesting level number
     * @param boolean Whether ot not this item is last, affects list item class
     * @param boolean Whether ot not this item is first, affects list item class
     * @param boolean Whether ot not this item is outermost, affects list item class
     * @param string Extra class of outermost list items
     * @param string If specified wraps children list in div with this class
     * @param boolean Whether ot not to add on* attributes to list item
     * @return string
     */
    protected function _renderCategorySelectOption($category, $level = 0, $isLast = false, $isFirst = false,
                                                   $isOutermost = false, $outermostItemClass = '', $childrenWrapClass = '', $noEventAttributes = false)
    {
        if (!$category->getIsActive()) {
            return '';
        }
        $html = array();

        // get all children
        if (Mage::helper('catalog/category_flat')->isEnabled()) {
            $children = (array)$category->getChildrenNodes();
        } else {
            $children = $category->getChildren();
        }

        // select active children
        $activeChildren = array();
        foreach ($children as $child) {
            if ($child->getIsActive()) {
                $activeChildren[] = $child;
            }
        }

        $active = '';
        if ($this->isCategoryActive($category)) {
            $active = 'selected="selected"';
        }
        // assemble list item with attributes
        $html[] = '<option value="'.$this->getCategoryUrl($category).'" '.$active.'>' . str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;',$level) . $this->escapeHtml($category->getName()) . '</option>';

        // render children
        $htmlChildren = '';
        foreach ($activeChildren as $child) {
            $childHtml = $this->_renderCategorySelectOption(
                $child,
                ($level + 1),
                0,
                0,
                false,
                $outermostItemClass,
                $childrenWrapClass,
                $noEventAttributes
            );
            $htmlChildren .= $childHtml;
        }

        if (!empty($htmlChildren)) {
            $html[] = $htmlChildren;
        }

        $html = implode("\n", $html);
        return $html;
    }


}

If you can see anything which maybe causing this problem could you let me know please as i am lost with this problem.

Thank you

  • inspect this subcategory, see if there is any supernumerary class added to the element. – Julien Lachal Oct 14 '14 at 12:02
  • The class change from inactive to active on the bottom category, same as when you are actually in the category – Adam Allen Oct 14 '14 at 12:19
  • Can you add either a image or add a url – Renon Stewart Oct 14 '14 at 14:00
  • @R.S what an screen shot of the problem? cant give out URL because the site isnt live yet and is password protected – Adam Allen Oct 14 '14 at 14:01
  • Probably the function _isCurrentCategory gets wrong category $cat = $this->getCurrentCategory(); from the registry? What does this code return? – Amasty Oct 14 '14 at 14:21
0

This could be a CSS related issue, please try to find the exact CSS being used for that block and try to see if any Bold font design has been written over there!

  • There is CSS because we need it to bold when the user is in the category. The problem is the bottom category is set to active when you are not in it. – Adam Allen Oct 17 '14 at 10:32
  • The return in _isCurrentCategory($category), is it giving required output? place a log to check – Rahul Kumar Das Oct 17 '14 at 10:36
  • What do you mean is it giving required output? If you mean does it work as we want. Then yes, the parent category is bold when you are in it and so are the children categories. The problem is when you are in a parent category it is also setting the last child category to active as well even when you are not in it – Adam Allen Oct 17 '14 at 10:39
  • From frontend, could you check that which css property is making the bottom category BOLD? – Rahul Kumar Das Oct 17 '14 at 10:43
  • .col-left .block-left-nav li.active > a { font-weight: bold; } – Adam Allen Oct 17 '14 at 10:45
0

Maybe you're just getting this active class through some JavaScript code on frontend? I would start from checking all JavaScript related to the menu.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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