【问题标题】:How to retrieve the parent category images in Topmenu in magento?如何在magento的Topmenu中检索父类别图像?
【发布时间】:2013-03-14 15:44:51
【问题描述】:

ma​​gento中如何检索Topmenu中的父类图片。当单击或鼠标悬停在受尊敬的父菜单上时,我必须显示父类别图像。我尝试了下面提到的代码,但我得到了所有的类别图像。如果我在前端显示它都显示在受尊重的菜单下。谁能指导我如何显示正确的图像?我的 magento 版本是 1.7.0.2。

$categoryData = 数组(

            'name' => $category->getName(),
            'id' => $nodeId,
            'url' => Mage::helper('catalog/category')->getCategoryUrl($category),
            'is_active' => $this->_isActiveMenuCategory($category),
            'links' => $cat->getData('links'),
            'image' => $cat->getImageUrl('image'),
            'thumbnail' => $cat->getThumbnail(),
            'getLevel' => $category->getLevel()
        );

我想用缩略图图片来展示。提前谢谢...

【问题讨论】:

    标签: magento zend-framework menu magento-1.7 categories


    【解决方案1】:

    最后我得到了解决方案并在下面分享答案。我可以使用 'getLevel' 属性来查找所有菜单的位置。然后我找出父菜单并在 Topmenu 中显示受人尊敬的图像。

    它被添加到模型文件中。 (/app/code/core/Mage/Catalog/Model/Observer.php)

    函数名称: _addCategoriesToMenu

    $categoryData = array(
    
                    'name' => $category->getName(),
                    'id' => $nodeId,
                    'url' => Mage::helper('catalog/category')->getCategoryUrl($category),
                    'is_active' => $this->_isActiveMenuCategory($category),
                    'links' => $cat->getData('links'),
                    'image' => $cat->getImageUrl('image'),
                    'thumbnail' => $cat->getThumbnail(),
                    'getLevel' => $category->getLevel()
                );
    

    它被添加到 Html 文件夹中。 (app/code/core/Mage/Page/Block/Html/Topmenu.php)

    函数名称: _getHtml

    $parentLevels = $child->getLevel();
    
            if($parentLevels == 0) 
            {
    
                $urls = Mage::getBaseUrl('media').'catalog/category/'.$child->getData('thumbnail');
    
                $html .= '<img src="'.$urls.'" />';
            }
    

    【讨论】:

      【解决方案2】:

      此解决方案适用于 Magento -1.8.*

      在模型文件中。 (/app/code/core/Mage/Catalog/Model/Observer.php)

      更新函数名称中的以下代码:_addCategoriesToMenu

            $categoryData = array( 
                  'name' => $category->getName(),
                  'id' => $nodeId,
                  'url' => Mage::helper('catalog/category')->getCategoryUrl($category),
                  'is_active' => $this->_isActiveMenuCategory($category),
                  'thumbnail' => Mage::getModel('catalog/category')->load($category->getId())->getThumbnail()
      
              );
      

      然后进入 Html 文件夹。 (app/code/core/Mage/Page/Block/Html/Topmenu.php)

      在第 128 行更新以下代码行

      函数名:_getHtml

              if($childLevel < 1 ){
                  $urls = Mage::getBaseUrl('media').'catalog/category/'.$child->getData('thumbnail');
                  $img = '<img src="'.$urls.'" />';
              }
      
              $html .= '<li ' . $this->_getRenderedMenuItemAttributes($child) . '>';
              $html .= '<a href="' . $child->getUrl() . '" ' . $outermostClassCode . '><span>'
                  . $this->escapeHtml($child->getName()) . ' </span> '.$img.' </a>';
      

      【讨论】:

      • 在该函数中使用 ->load() 是一个非常糟糕的主意,因为它会一遍又一遍地循环。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-04
      • 1970-01-01
      相关资源
      最近更新 更多