0

I am working with display all categories on a custom page, either it is enabled or disabled.

When I try to get child categories with "getChildrenCategories()", but it is returning only enabled child categories it is not returning the disabled child categories. I need both types of child categories.

How can I get the all child categories which are disabled/enabled?

please check the code as below.

protected $_categoryFactory;
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Catalog\Model\CategoryFactory $categoryFactory
){
    parent::__construct($context);
    $this->_categoryFactory = $categoryFactory;
}

public function getAllChildCategories($categoryId)
{
    $categoryObj = $this->_categoryFactory->create()->load($categoryId);
    if($categoryObj->hasChildren()) {
        return $categoryObj->getChildrenCategories();
    }

}
0

Default children categories only return as active. You need to that function changes to get the categories that you want to override.

vendor/magento/module-catalog/Model/ResourceModel/Category.php

From

public function getChildrenCategories($category)
{
    $collection = $category->getCollection();
    /* @var $collection \Magento\Catalog\Model\ResourceModel\Category\Collection */
    $collection->addAttributeToSelect(
        'url_key'
    )->addAttributeToSelect(
        'name'
    )->addAttributeToSelect(
        'all_children'
    )->addAttributeToSelect(
        'is_anchor'
    )->addAttributeToFilter(
        'is_active',
        1
    )->addIdFilter(
        $category->getChildren()
    )->setOrder(
        'position',
        \Magento\Framework\DB\Select::SQL_ASC
    )->joinUrlRewrite();

    return $collection;
}

To

public function getChildrenCategories($category)
{
    $collection = $category->getCollection();
    /* @var $collection \Magento\Catalog\Model\ResourceModel\Category\Collection */
    $collection->addAttributeToSelect(
        'url_key'
    )->addAttributeToSelect(
        'name'
    )->addAttributeToSelect(
        'all_children'
    )->addAttributeToSelect(
        'is_anchor'
    )->addIdFilter(
        $category->getChildren()
    )->setOrder(
        'position',
        \Magento\Framework\DB\Select::SQL_ASC
    )->joinUrlRewrite();

    return $collection;
}

From the above functions remove "is_active" filter too that function

| improve this answer | |
0

Please try with below code.

public function __construct(
        Template\Context $context, 
        \Magento\Catalog\Model\Layer\Resolver $layerResolver, 
        \Magento\Framework\Registry $registry, 
        \Magento\Catalog\Helper\Category $categoryHelper, 
        \Magento\Catalog\Model\CategoryFactory  $categoryFactory,
        array $data = array()) 
    {
        parent::__construct($context, $layerResolver, $registry, $categoryHelper,$data);
        $this->_categoryFactory = $categoryFactory;
        $this->_collectionFactory = $collectionFactory;
     }

    public function getCategoryList()
    {
      $_category  = $this->getCurrentCategory();
      $collection = $this->_categoryFactory->create()->getCollection()->addAttributeToSelect('*')
              ->setOrder('position', 'ASC')
              ->addIdFilter($_category->getChildren());
      return $collection;

    }

I hope its work for you.

| improve this answer | |
  • I need enabled and disabled both categories. this will only get enabled/active category. – Utsav Gupta Jul 24 '19 at 9:46
  • please check with it, i update collection, try with remove filter is_Active, hope its work for you. – Anas Mansuri Jul 24 '19 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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