【问题标题】:How to add category image/thumbnail to navigation in Magento > 1.7如何在 Magento > 1.7 中将类别图像/缩略图添加到导航中
【发布时间】:2012-09-20 14:22:46
【问题描述】:

我已经看到了一些针对 Magento top.phtml 移动到 \app\design\frontend\THEME\TEMPLATENAME\template\page\html\topmenu.phtml,因此我看到的解决方案不再适用。

我只想能够在原生类别菜单下拉列表中输出类别图像(通过管理界面上传)。下拉结构和布局已完成并按我的意愿工作,减去图像。

【问题讨论】:

  • 不清楚您是指高于 1.7 的版本还是高于 1.7 的版本。你的标题是一个,但问题是另一个。

标签: php magento


【解决方案1】:

您需要覆盖/替换 Mage_Catalog_Model_Observer。

最简单的方法是将Mage_Catalog_Model_Observer (app/code/core/Mage/Catalog/Model/Observer.php) 复制到:

 app/code/local/Mage/Catalog/Model/Observer.php

然后您可以修改:_addCategoriesToMenu()

将图像添加到数据中:

$categoryData = array(
    'image_url' => $category->getImageUrl(), // or thumbnail if you wanted.
    'name' => $category->getName(),
    'id' => $nodeId,
    'url' => Mage::helper('catalog/category')->getCategoryUrl($category),
    'is_active' => $this->_isActiveMenuCategory($category)
);

然后,此数据将在导航块中可用。您还需要覆盖此块:将 app/code/core/Mage/Catalog/Block/Navigation.php 复制到

app/code/local/Mage/Catalog/Block/Navigation.php

修改_getHtml()方法,根据需要将图片添加到标记中。

图像 url 将通过节点可用,如下所示:

$child->getImageUrl(); // or
$child->getData('image_url');

【讨论】:

  • 我无法让它工作。您的推荐没有任何回报。哦,getHtml() 方法现在位于/app/code/core/Mage/Page/Block/Html/Topmenu.php。我最接近解决方案的是类别页面本身,使用<img alt="" src="<?php echo Mage::getBaseUrl('media') . 'catalog/category/' . $_category->getThumbnail(); ?>"> 输出缩略图,但这在尝试在导航菜单中获取子目录缩略图时没有多大帮助。
【解决方案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

$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>';

【讨论】:

    猜你喜欢
    • 2012-05-25
    • 1970-01-01
    • 2017-11-06
    • 2012-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多