You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

覆写Codazon MegaMenu模块Block后,分类链接title属性未生效

问题描述

我们使用Codazon MegaMenu模块作为主菜单,但菜单链接没有title标签。希望通过覆写模块代码,为分类链接添加title属性,值为分类名称。

已创建自定义模块,复制并修改了生成分类链接标签的_getHtml方法来添加title属性,但刷新页面后修改始终不生效。已执行缓存清理、模块升级和静态文件清理操作。

可能的原因及排查步骤
  • 模块未正确启用:执行命令 bin/magento module:status MyModule_MegaMenu 查看模块状态,若未启用则执行 bin/magento module:enable MyModule_MegaMenu 并重新升级模块。
  • 类重写被覆盖:其他模块可能也重写了同一个Codazon\MegaMenu\Block\Widget\CategoriesTree类,导致你的重写优先级更低。执行命令 bin/magento info:di:preference Codazon\MegaMenu\Block\Widget\CategoriesTree,查看最终生效的重写类是否是你的MyModule\MegaMenu\Block\Widget\CategoriesTree。
  • 方法重写签名不匹配:检查原Codazon模块的_getHtml方法签名(参数、返回值、访问修饰符)是否和你重写的完全一致。如果原模块有版本更新,方法结构可能发生变化,导致你的重写未被正确调用。
  • 缓存清理不彻底:除了常规缓存清理,手动删除以下目录确保彻底清理:
    • var/cache/
    • var/page_cache/
    • generated/code/
    • generated/metadata/
      之后重新执行缓存清理命令。
  • 模板文件替代了Block逻辑:Codazon MegaMenu可能使用自定义模板渲染菜单,而非直接调用Block的_getHtml方法。检查原模块的模板文件(如app/code/Codazon/MegaMenu/view/frontend/templates/widget/categories_tree.phtml),看是否在模板中单独渲染链接,导致Block的修改未生效。
  • 生产模式下未重新编译:如果站点处于生产模式,修改代码后需要执行 bin/magento setup:di:compile 重新编译代码,否则修改不会被应用。
自定义模块代码

Block/Widget/CategoriesTree.php

<?php

namespace MyModule\MegaMenu\Block\Widget;

use Codazon\MegaMenu\Block\Widget\CategoriesTree as OriginalCategoriesTree;

class CategoriesTree extends OriginalCategoriesTree
{
    protected function _getHtml(
        \Magento\Framework\Data\Tree\Node $menuTree,
        $childrenWrapClass,
        $limit,
        $colBrakes = []
    ) {
        $html = '';
        $col = 1;
        
        $itemCount = $this->getData('item_count');
        
        $children = $menuTree->getChildren();
        $parentLevel = $menuTree->getLevel();
        $childLevel = ($parentLevel === null) ? 1 : $parentLevel + 1;

        $counter = 1;
        $itemPosition = 1;
        $childrenCount = $children->count();

        
        $parentPositionClass = $menuTree->getPositionClass();
        $itemPositionClassPrefix = $parentPositionClass ? $parentPositionClass . '-' : 'nav-';

        foreach ($children as $child) {
            $child->setLevel($childLevel);
            $child->setIsFirst($counter == 1);
            $child->setIsLast($counter == $childrenCount);
            $child->setPositionClass($itemPositionClassPrefix . $counter);

            $outermostClassCode = '';
            $outermostClass = $menuTree->getOutermostClass();

            if ($childLevel == 0 && $outermostClass) {
                $outermostClassCode = ' class="' . $outermostClass . '" ';
                $child->setClass($outermostClass);
            }

            if ($this->shouldAddNewColumn($colBrakes, $counter)) {
                $col = 24/ceil($childrenCount/$limit);
                $html .= '</ul></li><li class="col-sm-'.$col.'"><ul>';
            }

            $html .= '<li ' . $this->_getRenderedMenuItemAttributes($child) . '>';
            $html .= '<a class="menu-link" href="' . $child->getUrl() . '" ' . $outermostClassCode . ' title="' . $this->escapeHtml($child->getName()) . '"><span>' . $this->escapeHtml(
                $child->getName()
            ) . '</span></a>' . $this->_addSubMenu(
                $child,
                $childLevel,
                $childrenWrapClass,
                $limit
            ) . '</li>';

            $itemPosition++;
            if($itemCount){
                if($itemCount == $counter){
                    break;      
                }
            }
            $counter++;
        }

        if (is_array($colBrakes) && count($colBrakes) && $limit) {
            $html = '<li class="col-sm-'.$col.'"><ul>' . $html . '</ul></li>';
        }

        return $html;
    }   
}

etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Codazon\MegaMenu\Block\Widget\CategoriesTree" type="MyModule\MegaMenu\Block\Widget\CategoriesTree" />
</config>

etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="MyModule_MegaMenu" setup_version="1.0.0">
        <sequence>
            <module name="Codazon_MegaMenu"/>
        </sequence>
    </module>
</config>

内容的提问来源于stack exchange,提问作者Midnightchems

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 03:07:15