我正在尝试在 Magento CE 1.8.1 的本地安装中添加一个新的根类别,但是当我按下保存类别按钮时,我在控制台中收到以下错误并且屏幕上没有任何反应。
我试图重新安装所有核心文件等,但似乎没有解决这个问题。
Uncaught TypeError: Cannot read property 'split' of undefined
我正在尝试在 Magento CE 1.8.1 的本地安装中添加一个新的根类别,但是当我按下保存类别按钮时,我在控制台中收到以下错误并且屏幕上没有任何反应。
我试图重新安装所有核心文件等,但似乎没有解决这个问题。
Uncaught TypeError: Cannot read property 'split' of undefined
这是将表单数据发送到 Magento 服务器的 ajax 例程中的 Javascript 错误。导致错误的代码是
var path = params['general[path]'].split('/');
general[path] 表示类别层次结构,因此根类别应始终具有
params['general[path]'] = 1
但是子类别将具有其父类别的ID。
你会得到一个奇怪的错误。你能成功地制作子类别吗?你能弄清楚为什么表单提交没有设置字段 general[path] 吗?如果您检查“添加新根类别页面”的 HTML 页面源代码,您应该会看到类似这样的代码,不是吗?
<input id="group_4path" type="hidden" value="1" name="general[path]">
您收到的错误表明您的新根类别表单中没有该行 HTML。(或者在此之前可能存在关于设置类别路径的 Javascript 错误,但首先要查找该 HTML 并请报告。您可以添加一些 JavaScript 断点来检查变量并尝试了解为什么 general[path ] 最终是未定义的。)
开始于Mage_Adminhtml_Block_Catalog_Category_Tab_Attributes
_prepareForm
函数中有一个 if-condition ( ) if ($this->getAddHiddenFields())
,它确保隐藏字段不会被渲染general[id]
,general[path]
因为它总是返回false
。
一个不好的解决方案是删除 if 条件。
但是由于核心变化很糟糕,新的奇迹是什么getAddHiddenFields()
以及为什么它会返回false
?
在数据库表中eav_attribute_group
搜索与以下查询匹配的条目:
SELECT * FROM `eav_attribute_group` WHERE default_id = 1 AND sort_order > 1;
并将 设置sort_order
为 1
我的第一个问题(什么是getAddHiddenFields()
)的答案:
getAddHiddenFields()
是一种神奇的方法,并返回 varien object field 的值'add_hidden_fields'
。的值'add_hidden_fields'
由setAddHiddenFields()
in设置Mage_Adminhtml_Block_Catalog_Category_Tabs->_prepareLayout()
。
为了回答我的第二个问题(为什么它总是返回false
),我创建了一个小调试日志:
# Debug log of Mage_Adminhtml_Block_Catalog_Category_Tabs->_prepareLayout()
init $defaultGroupId with: 0
check group 157 is 0 or isDefault //Note 1 (see further down below)
if ($defaultGroupId(0) == 0 or $group->getIsDefault():false)
set $defaultGroupId to 157
check group 3 is 0 or isDefault
if ($defaultGroupId(157) == 0 or $group->getIsDefault():false) //Note 2 (see further down below)
check group 10 is 0 or isDefault
if ($defaultGroupId(157) == 0 or $group->getIsDefault():false)
[...]
process groupId 157
groupId 157 has no attributes
if (!$attributes) { continue; }
process groupId 3
groupId 3 has attributes
if (!$attributes) { continue; }
$active = $defaultGroupId == $group->getId();
setAddHiddenFields($active (false)))
process groupId 10
groupId 10 has attributes
if (!$attributes) { continue; }
setAddHiddenFields($active (false)))
[...]
注意 1: 记住 $defaultGroupId 初始化为 0,因此 groupCollection 的第一个条目将设置为默认值(因此,当前的解决方案是将 defaultGroups sortOrder 设置为 1)
注意 2: 哦,看看第 3 组的 nextmystery $group->getIsDefault() 返回 FALSE(在我的情况下是第 3 组 General 并且在数据库中 is_default = 1)我还没有测试过,因为目前的解决方案对我来说已经足够了.