1

i tryed get all category in phtml by this code, but it not working, it get all category, not only category active. Please help me. Thanks all.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$categoryFactory = $objectManager->create('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
$storeManager = $objectManager->create('\Magento\Store\Model\StoreManagerInterface');
$categories = $categoryFactory->create()                              
    ->addAttributeToSelect('*')->addFieldToFilter('is_active', 1)
    ->setStore($storeManager->getStore());
1
  1. You need to put below code into your block file.

    <?php
    namespace Vendor\Extension\Block;
    
    class Categoryinfo extends \Magento\Framework\View\Element\Template
    {    
        protected $_categoryCollectionFactory;
    
        public function __construct(
            \Magento\Framework\View\Element\Template\Context $context,        
            \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
            array $data = []
        )
        {
             $this->_categoryCollectionFactory = $categoryCollectionFactory;
             parent::__construct($context, $data);
        }
    
    
        public function getActiveCategoryCollection()
        {
            $collection = $this->_categoryCollectionFactory->create();
            $collection->addAttributeToSelect('*');        
            $collection->addIsActiveFilter();
            return $collection;
        }
    }
    ?>
    
  2. Now into your phtml file you can call those function like this :

    $categories = $block->getActiveCategoryCollection();
    foerach($categories as $category)
    {
        // DO YOUR LOGIC
    } 
    
0

Create your block and put below code in block php file

<?php
namespace Module\Example\Block;

class Categorydata extends \Magento\Framework\View\Element\Template 
{
public function getCategories()
{
$categoryFactory = $_objectManager->create('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
$categories = $categoryFactory->create()                              
    ->addAttributeToSelect('*')->addFieldToFilter('is_active', 1);
return $categories;
}
}
?>

after that paste below code into view phtml file.

<?php 
    $collection = $block->getCategories();
    foreach ($collection as $categorycollection){
        $categorycollection->getName();
    }
   ?>
0

If you need active or inactive both categories so used this code.

        <?php 
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $categoryFactory = $objectManager->create('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
        $storeManager = $objectManager->create('\Magento\Store\Model\StoreManagerInterface');
        $categories = $categoryFactory->create()->addAttributeToSelect('*')->setStore($storeManager->getStore());
        foreach ($categories as $cat) {
            echo "<pre>";
            echo "Category Id - ".$cat->getId();
            echo "Category Name - ".$cat->getName();     
        }

        ?>
0

you try Following way..

<?php 
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

// get the list of all categories
$categoryCollection = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
$categories = $categoryCollection->create();
$categories->addAttributeToSelect('*');

foreach ($categories as $category) {
    if ($category->getIsActive()){
        echo $category->getId() . '<br />';
        echo $category->getName() . '<br />';
        echo $category->getUrl() . '<br />';
    }
}

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.