Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento - JavaScript error prevents adding and saving of categories

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

like image 338
Dave Sims Avatar asked Mar 26 '14 17:03

Dave Sims


2 Answers

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.)

like image 181
Malachy Avatar answered Nov 07 '22 03:11

Malachy


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.

like image 1
rayphi Avatar answered Nov 07 '22 04:11

rayphi