9

I am trying to add a new root category on a local install of Magento CE 1.8.1, however when I press the save category button, I get the following error in the console and nothing happens on screen.

I have tried to reinstall all the core files etc but nothing seems to fix this issue.

Uncaught TypeError: Cannot read property 'split' of undefined

rayphi
  • 503
  • 2
  • 14
  • 30
Dave Sims
  • 735
  • 1
  • 6
  • 11

2 Answers2

1

This is a Javascript error in the ajax routine that sends the form data to the Magento server. The code that is causing the error is

var path = params['general[path]'].split('/');

the general[path] represents the category hierarchy so a root category should always have a

params['general[path]'] = 1

but a sub category will have the id of it's parent category.

It is an odd error for you to get. Can you make sub categories successfully? Can you work out why the form submission is not setting the field general[path]? If you inspect the HTML page source of the 'add new root category page' you should see some code like this, no?

<input id="group_4path" type="hidden" value="1" name="general[path]">

The error you are getting suggests that you don't have that line of HTML in your new root category form. (Or possibly that there is a Javascript error prior to this, about setting the category path, but start by looking for that HTML and please report back. You could add some JavaScript break points to inspect the variables and try to understand why general[path] ends up being undefined.)

Malachy
  • 1,580
  • 1
  • 11
  • 9
1

The real problem

starts in Mage_Adminhtml_Block_Catalog_Category_Tab_Attributes

In the _prepareForm function is a if-condition (if ($this->getAddHiddenFields())) which ensures that the hidden fields general[id] and general[path] are not rendered because it always returns false.

A bad solution would be to remove the if condition.

but as core changes are bad, is the new wonder what is getAddHiddenFields() and why does it return false?

The Solution (for now):

In the database table eav_attribute_group search for an entry that matches the following query:

SELECT * FROM `eav_attribute_group` WHERE default_id = 1 AND sort_order > 1;

and Set the sort_order to 1

The Explanation:

The answer to my first question (what is getAddHiddenFields()): getAddHiddenFields() is a magic method and returns the value of the varien object field 'add_hidden_fields'. The Value of 'add_hidden_fields' is set by setAddHiddenFields() in Mage_Adminhtml_Block_Catalog_Category_Tabs->_prepareLayout().

For the answer to my second question (why does it always return false) i created a little Debug log:

# 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)))
[...]

Note 1: remember $defaultGroupId is initalized with 0 so the first entry of groupCollection would be set as default (Because of this the current solution is to set the defaultGroups sortOrder to 1)

Note 2: Oh look the nextmystery $group->getIsDefault() of group 3 returns FALSE (in my case is group 3 General and in the Database is_default = 1) I have not tested yet, because the current solution is currently sufficient for me.

Community
  • 1
  • 1
rayphi
  • 503
  • 2
  • 14
  • 30
  • actually the issue seems to be that the $active is decided upon this group having attributes set or not , and it does not have those set and this fails . this query won't solve the issue – Anton S Jan 27 '16 at 10:25
  • i think the issue is that $group->getIsDefault() for group id 3 returns false although it is the default group. Would $group->getIsDefault() return true, $active would also be true. Except if it were possible that the default group has no attributes. – rayphi Jan 28 '16 at 11:51
  • see the code it does continue; if no attributes and default group is skipped from there and thus it fails – Anton S Jan 28 '16 at 15:32
  • but only the group with id 157 has no attributes the real defaultGroup (3) has attributes and didn't continue at this point. The issue is `if ($defaultGroupId == 0 or $group->getIsDefault())` $defaultGroupId where initialized with 0 so $defaultGroupId bcomes 157 wich is wrong because it is not the default group – rayphi Jan 29 '16 at 08:23
  • i'm hitting against the case where defaultgroup does not have any attributes – Anton S Jan 29 '16 at 09:26
  • in this case you are right but this case would mean that only an empty tab appears. This is the reason of `if (!$attributes) { continue; }` – rayphi Jan 29 '16 at 11:43
  • +1 for the detailed description. We were stuck with an issue for hours. Searching for the method name "setAddHiddenFields" brought me here and was the final solution of a complex puzzle. Thanks for the effort you put in explaining it 5 years ago :) – Akif Apr 30 '21 at 19:18
  • I'm glad it helped you guys! ;) – rayphi May 03 '21 at 09:14