导航中的 Magento 类别和产品 - 子项

Posted

技术标签:

【中文标题】导航中的 Magento 类别和产品 - 子项【英文标题】:Magento categories and products in navigation - child 【发布时间】:2014-02-19 03:09:57 【问题描述】:

我正在使用某种 magento 导航调整,它在主导航中显示类别和产品:

但有一个问题:在我的类别中:

Theme supplies
 -VIP Sparklers (2)
 -UV / Glow (12)
 -Seasonal (6)
 --Summer (6)
 -Confetti Cannons (7)
 -Props (2)

问题在于“夏季”子类别,它以这种方式显示:

如何以其他方式显示它,就像其他类别一样?像这样:

SEASONAL
SUMMER
Product1
2
3
4
5
6

这是我的navigation.php

<?php
/**
 * @version   1.0 12.0.2012
 * @author    Olegnax http://www.olegnax.com <mail@olegnax.com>
 * @copyright Copyright (C) 2010 - 2012 Olegnax
 */

class Olegnax_Navigation_Block_Navigation extends Mage_Catalog_Block_Navigation


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

    /**
     * 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>';

    // Grabbing the products for the category if it's level is 1
    if ($level == 1) 

        $catId = $category->getId();
        $categorie = new Mage_Catalog_Model_Category();
        $categorie->load($catId); // this is category id
        $collection = $categorie->getProductCollection()->addAttributeToSort('name', 'asc');
        $html[] = '<ul>';

        foreach ($collection as $pc)
        
            $p = new Mage_Catalog_Model_Product();
            $p->load($pc->getId());

            $data = $p->_data;
            $html[] = '<li><a href="/shop/'.$data['url_path'].'">'.$data['name'] .'</a></li>';
        

        $html[] = "</ul>\n";

    
    // Done

    // render children
    $htmlChildren = '';
    $j = 0;
    foreach ($activeChildren as $child) 
        $htmlChildren .= $this->_renderCategoryMenuItemHtml(
            $child,
            ($level + 1),
            ($j == $activeChildrenCount - 1),
            ($j == 0),
            false,
            $outermostItemClass,
            $childrenWrapClass,
            $noEventAttributes
        );
        $j++;
    
    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;




【问题讨论】:

【参考方案1】:

我认为this is basically what you're looking for。几年前我在一个项目中遇到了这个问题,这就是我解决它的方法。它允许在下拉菜单中使用图片轻松添加类别及其各自的产品。

更新

认为你想删除这些行,但我不肯定:

if ($hasActiveChildren && !$noEventAttributes) 
     $attributes['onmouseover'] = 'toggleMenu(this,1)';
     $attributes['onmouseout'] = 'toggleMenu(this,0)';

【讨论】:

是的,这很酷,但是 1 个小问题,看看:oi42.tinypic.com/20870i8.jpg 我不需要这个额外的框,你的代码有什么变化来删除它? 看起来该框出现在鼠标悬停/悬停时正确吗?如果是这样,请参阅我的更新答案。 是的,鼠标悬停在季节性和每个产品上。从您的编辑中删除代码什么都不做:( 哦,我看到如果启用此脚本,它会使页脚下的整个页面高度非常非常长,我在 css 检查器中看不到它,我更改为默认导航文件,页面高度为很好 我无法对此进行测试,因此您需要尝试删除代码块。我怀疑它是 cmets render children 下的最后两个主要块之一。

以上是关于导航中的 Magento 类别和产品 - 子项的主要内容,如果未能解决你的问题,请参考以下文章

Magento 主题例外不适用于类别和产品页面

Magento:如何在主导航菜单的下拉菜单中添加活动产品

所有产品(类别和子类别产品)都计入 magento 中的类别列表页面

如何使magento过滤器像类别一样工作

无法在 magento 2.4.3 中查看类别下的产品

从Magento中的分层导航取消设置类别