3

I've been playing with the Catalog classes for a while, deleting and importing root categories, subcategories, stores and store groups, and I just discovered that every time I delete a root category the root catalog children count is decreasing.

I get this using $rootCatalog->getChildrenCount() and it has now reached -249.

I must have done something dirty somewhere in my process, but I cannot find the source of this strange behavior.

This is the way I create root categories :

public function createRootCategory($name)
    {
        $parentCategory = $this->_categoryFactory->create()->load(\Magento\Catalog\Model\Category::TREE_ROOT_ID);
        $category = $this->_categoryFactory->create();

        $category->setPath($parentCategory->getPath());
        $category->setStoreId(0);
        $category->setName($name);
        $category->setIsActive(1);
        $category->setDisplayMode('PRODUCTS');
        $category->setParentId(\Magento\Catalog\Model\Category::TREE_ROOT_ID);
        $category->save();

        return $category->getId();
    }

Do you have any clue about the source of this behavior?

Do you know if it may impact Magento in any way?

How do I solve this?

2 Answers 2

1

The most likely cause of this behavior is the _beforeDelete method in \Magento\Catalog\Model\ResourceModel\Category, specifically this line:

$this->getAggregateCount()->processDelete($object);

This will run processDelete() within \Magento\Catalog\Model\ResourceModel\Category\AggregateCount, which decreases children_count for all parent categories via:

$data = ['children_count' => new \Zend_Db_Expr('children_count - ' . $childDecrease)];
$where = ['entity_id IN(?)' => $parentIds];
$resourceModel->getConnection()->update($resourceModel->getEntityTable(), $data, $where);

However, it is weird that you end up with a negative number here, since the children_count should increase when you first assign a category as a child. It could be that your custom code never increments the value in the first place, so definitely debug this, you may be doing something which is not "the Magento way". Since I do not know what your code looks like, I cannot say for sure, but definitely have a look at the core logic of the class I mentioned above.

You should fix the value manually after you have figured out what the problem was just to be on the safe side.

3
  • Yep, that is the source for sure because I creating those root categories directly by creating new instances and assigning id's by hand (I edited my post, adding the code causing this issue)... thanks for that clue... now the question is : What is "The Magento way" for doing this ?
    – Gael COAT
    Commented Aug 28, 2016 at 16:47
  • As you said, I think that the probleme is mostly in the creation process (when the children count isn't increasing). I use delete to delete those root categories, and it decrease the count with success.
    – Gael COAT
    Commented Aug 28, 2016 at 17:19
  • Hey, apologies for the bit of confusion on my end, I blame Sunday evening! I am not 100% certain why your category creation is not working, to me it seems correct, but maybe you should double-check the core logic in \Magento\Catalog\Controller\Adminhtml\Category\Save in the execute() method. If you cannot find any obvious code issues, I suggest you use a debugger with PHPStorm or similar and compare the data values which the core controller sets when creating a category and compare that to what you set, maybe something essential is missing.
    – TiEul
    Commented Aug 28, 2016 at 17:49
0
<?php
$categoryCollection = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');

$categories = $categoryCollection->create();

$categories->addAttributeToSelect('*');
$categories->addAttributeToFilter('level', array('eq'=>2));
$categories->load();

foreach($categories as $category):

    $catId = $category->getId();

    $category = $objectManager->create('Magento\Catalog\Model\CategoryFactory')->create()->setStoreId(0)->load($catId);

    $childrenCategories = $category->getChildrenCategories();

        if(!$childrenCategories->count()) continue; //Chiled Category Count
            $CategoryName = $category->getName();
            echo "<pre>";
            print_r($CategoryName);
            echo "<pre>";
    endforeach;
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.