Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 3 of 3
Magento 1.5 Admin - categories tree for products does not auto-expand to show product categories
 
Tamburra
Member
 
Total Posts:  46
Joined:  2009-12-02
 

I have recently updated our 1.6.2.0 store to 1.7.0.2 and am experiencing this same issue.  The category tree within the product edit page is not expanding to show the selected categories.

I have deleted all categories and then added some testing categories and am experiencing the same issue.
I know for a fact that this is a database issue as I have pointed our upgraded database to a clean install of 1.7.0.2 with sample data and everything is working fine.  All categories are auto expanded.
It is only causing issues on the database that we upgraded from 1.6.2.0.
Has anyone had this same problem and found a solution?

Of course on a clean install everything works fine.

 
Magento Community Magento Community
Magento Community
Magento Community
 
malalaraf
Jr. Member
 
Total Posts:  5
Joined:  2010-08-19
 
bepixeld - 14 July 2012 02:34 AM

I found another solution for 1.7.0.2

copy /app/code/core/Mage/Adminhtml/Block/Catalog/Product/Edit/Tab/Categories.php
to /app/code/local/Mage/Adminhtml/Block/Catalog/Product/Edit/Tab/Categories.php
So we don\\\’t edit core files.

Edit /app/code/local/Mage/Adminhtml/Block/Catalog/Product/Edit/Tab/Categories.php
the function _isParentSelectedCategory() around line 190
replace the code:

...
        
$allChildren $node->getAllChildren();
        if (
$allChildren{
            $selectedCategoryIds 
$this->getCategoryIds();
            
$allChildrenArr explode(\\\',\\\'$allChildren);
...

with:
...
        
$allChildren $node->getAllChildNodes();
        if (
$allChildren{
            $selectedCategoryIds 
$this->getCategoryIds();
            
$allChildrenArr array_keys($allChildren);
...
$node->getAllChildren() is in my cases alway null, so i get no result.
In $node->getAllChildNodes() we get the category entity_ids as keys of the array, so we can use this.

And then in function getSelectedCategoriesPathIds() around line 275
replace the code:
...
        if (
$rootId{
            $collection
->addFieldToFilter(\\\'parent_id\\\'$rootId);
        
else {
            $collection
->addFieldToFilter(\\\'entity_id\\\', array(\\\'in\\\'=>$categoryIds));
        
}
...

with:
...
        
$collection->addFieldToFilter(\\\'entity_id\\\', array(\\\'in\\\'=>$categoryIds));
...
The collection-filter with $rootId is not working in some cases for me.
But the collection-filter $categoryIds is always working. In single store and multi store.
This solution expands the nodes in PHP level.
I hope this helps someone.

Multi-store Magento ver. 1.7.0.2
Above worked for me
Thx

 
Magento Community Magento Community
Magento Community
Magento Community
 
xiedean
Jr. Member
 
Total Posts:  2
Joined:  2010-03-07
 

Rewrite Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Categories

protected function _isParentSelectedCategory($node)
{
$result = false;
// Contains string with all category IDs of children (not exactly direct) of the node
// $allChildren = $node->getAllChildren();
$allChildren = Mage::getModel(’catalog/category’)
->load($node->getId())
->getAllChildren();

if ($allChildren) {
$selectedCategoryIds = $this->getCategoryIds();
$allChildrenArr = explode(’,’, $allChildren);
for ($i = 0, $cnt = count($selectedCategoryIds); $i < $cnt; $i++) {
$isSelf = $node->getId() == $selectedCategoryIds[$i];
if (!$isSelf && in_array($selectedCategoryIds[$i], $allChildrenArr)) {
$result = true;
break;
}
}

That will be OK.

 
Magento Community Magento Community
Magento Community
Magento Community
 
gadz
Jr. Member
 
Total Posts:  3
Joined:  2012-09-02
 

I have the same problem, after I upgrade into magento 1.7.0.2 , I have tried the code above but still it won’t auto-expand the categories tree of the product. can anyone help me out about this? thanks in advance ...

 
Magento Community Magento Community
Magento Community
Magento Community
 
Yireo
Member
 
Avatar
Total Posts:  52
Joined:  2008-05-26
The Netherland
 

Hi all,

We had this same issue, and the trick of the “children_count” field being a negative number, did the trick. Here’s a code segment that we used to automatically correct all values for all categories:

require_once 'app/Mage.php';
Mage::app();
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$categories Mage::getModel('catalog/category')->getCollection();
foreach(
$categories as $category{
    $children 
Mage::getModel('catalog/category')->getCollection()
        ->
addAttributeToFilter('parent_id'2)
    ;
    
$category->setData('children_count'$children->getSize());
    
$category->save();
}

We ran this a stand-alone PHP-script, from a shell to prevent timeouts.

 
Magento Community Magento Community
Magento Community
Magento Community
 
balko16
Jr. Member
 
Total Posts:  1
Joined:  2013-01-15
 
rrroulio - 16 April 2011 05:41 PM

I have similar pb in all my websites in 1.5
one function is different from 1.4.2 and 1.5 in :
app/code/core/Mage/Adminhtml/Block/Catalog/Product/Edit/Tab/Categories.php

i have changed it by the 1.4.2 code and yeahh; it works fine. Categories are auto expanded

change the last fonction by this code.

public function getSelectedCategoriesPathIds($rootId false)
    
{
        $ids 
= array();
        
$collection Mage::getModel('catalog/category')->getCollection()
            ->
addFieldToFilter('entity_id', array('in'=>$this->getCategoryIds()));
        foreach (
$collection as $item{
            
if ($rootId && !in_array($rootId$item->getPathIds())) {
                
continue;
            
}
            
foreach ($item->getPathIds() as $id{
                
if (!in_array($id$ids)) {
                    $ids[] 
$id;
                
}
            }
        }
        
return $ids;
    
}

that did it also in 1.7.0.2 ... THX

 
Magento Community Magento Community
Magento Community
Magento Community
 
harsh1212
Jr. Member
 
Total Posts:  21
Joined:  2010-07-20
 

Hi Wyomind,

Thanks for the help.

Your solution works like a charm.

I am using ver. 1.7.0.2

categoryLoader.on("load", function(treeLoadernodeconfig{
        varienWindowOnload
();
        
node.childNodes.each(function(e){
          
if($('product_categories').value.split(',').indexOf(e.attributes.id)!=-1)
            
e.getUI().check(true);
        
})
    
});

Thanks again,

Harsh

 
Magento Community Magento Community
Magento Community
Magento Community
 
georgeduta
Jr. Member
 
Avatar
Total Posts:  1
Joined:  2012-08-07
Sangeorgiu de Mures
 
SonicE - 23 August 2011 07:07 AM

I think the issue is in another function in app/code/core/Mage/Adminhtml/Block/Catalog/Product/Edit/Tab/Categories.php

public function getRoot($parentNodeCategory=null$recursionLevel=3)
    
{
        
if (!is_null($parentNodeCategory) && $parentNodeCategory->getId()) {
            
return $this->getNode($parentNodeCategory$recursionLevel);
        
}
        $root 
Mage::registry('root');
        if (
is_null($root)) {
            $storeId 
= (int) $this->getRequest()->getParam('store');

            if (
$storeId{
                $store 
Mage::app()->getStore($storeId);
                
$rootId $store->getRootCategoryId();
            
}
            
else {
                $rootId 
Mage_Catalog_Model_Category::TREE_ROOT_ID;
            
}

            $ids 
$this->getSelectedCategoriesPathIds($rootId);
            
$tree Mage::getResourceSingleton('catalog/category_tree')
                ->
loadByIds($idsfalsefalse);

            if (
$this->getCategory()) {
                $tree
->loadEnsuredNodes($this->getCategory(), $tree->getNodeById($rootId));
            
}

            $tree
->addCollectionData($this->getCategoryCollection());

            
$root $tree->getNodeById($rootId);

            if (
$root && $rootId != Mage_Catalog_Model_Category::TREE_ROOT_ID{
                $root
->setIsVisible(true);
                if (
$this->isReadonly()) {
                    $root
->setDisabled(true);
                
}
            }
            
elseif($root && $root->getId() == Mage_Catalog_Model_Category::TREE_ROOT_ID{
                $root
->setName(Mage::helper('catalog')->__('Root'));
            
}

            Mage
::register('root'$root);
        
}

        
return $root;
    
}

Take a look at
$storeId = (int) $this->getRequest()->getParam('store');

On a single store view it always returns NULL and then the $rootId is not set correctly by calling getSelectedCategoriesPathIdss($rootId)

My fix is:

public function getRoot($parentNodeCategory=null$recursionLevel=3)
    
{
        
if (!is_null($parentNodeCategory) && $parentNodeCategory->getId()) {
            
return $this->getNode($parentNodeCategory$recursionLevel);
        
}
        $root 
Mage::registry('root');
        if (
is_null($root)) {
            $storeId 
= (int) $this->getRequest()->getParam('store');
//------>START FIX
            
if ($storeId{
                $store 
Mage::app()->getStore($storeId);
                
$rootId $store->getRootCategoryId();
                
$ids $this->getSelectedCategoriesPathIds($rootId); // Added this line, and commented below
            
}
            
else {
                $rootId 
Mage_Catalog_Model_Category::TREE_ROOT_ID;
                
$ids $this->getSelectedCategoriesPathIds(); // Added this line without passing $rootId , and commented below
            
}

            
//$ids = $this->getSelectedCategoriesPathIds($rootId); //Commented this line and moved up
//------>END FIX         
            
$tree Mage::getResourceSingleton('catalog/category_tree')
                ->
loadByIds($idsfalsefalse);

            if (
$this->getCategory()) {
                $tree
->loadEnsuredNodes($this->getCategory(), $tree->getNodeById($rootId));
            
}

            $tree
->addCollectionData($this->getCategoryCollection());

            
$root $tree->getNodeById($rootId);

            if (
$root && $rootId != Mage_Catalog_Model_Category::TREE_ROOT_ID{
                $root
->setIsVisible(true);
                if (
$this->isReadonly()) {
                    $root
->setDisabled(true);
                
}
            }
            
elseif($root && $root->getId() == Mage_Catalog_Model_Category::TREE_ROOT_ID{
                $root
->setName(Mage::helper('catalog')->__('Root'));
            
}

            Mage
::register('root'$root);
        
}

        
return $root;
    
}

Please correct me if all this is wrong

Worked for me! Magento 1.6.2.0

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 3 of 3