cancel
Showing results for 
Search instead for 
Did you mean: 

Not able to fatch all category in Magento 2.3.2

   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

Not able to fatch all category in Magento 2.3.2

Hi all,

 

i am trying to fatch all the category in magento 2.3.2 but its not working and through the errors. here are code :-

 

public function __construct(
\Magento\Catalog\Block\Product\Context $context,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
//\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
\Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
\Magento\Framework\App\Http\Context $httpContext,
array $data = []
) {
$this->_productCollectionFactory = $productCollectionFactory;
//$this->_categoryCollectionFactory = $categoryCollectionFactory;
$this->_catalogProductVisibility = $catalogProductVisibility;
$this->httpContext = $httpContext;
$this->_imageHelper = $context->getImageHelper();
$this->_cartHelper = $context->getCartHelper();
parent::__construct(
$context,
$data
);
$this->setCollection($this->getProductCollection());
//$this->setCollections($this->getCategoryCollection());
}

 

 

thanks

Anurag Saxena

https://www.artisticbird.com/onlineshops-magento

2 REPLIES 2

Re: Not able to fatch all category in Magento 2.3.2

Hi @anuragonweb 

 

I would suggest you to use below code to get all the categories :

 

protected $_storeManager;
protected $_categoryCollection;

public function __construct(
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollection,
    $data = []
) {
    $this->_storeManager = $storeManager;
    $this->_categoryCollection = $categoryCollection;
    parent::__construct($data);
}

public function getCategories(){
     $categories = $this->_categoryCollection->create()                              
         ->addAttributeToSelect('*')
         ->setStore($this->_storeManager->getStore()); //categories from current store will be fetched

     foreach ($categories as $category){
         $category->getName();
     }
 }

Hope it helps !

if issue solved,Click Kudos & Accept as Solution

Re: Not able to fatch all category in Magento 2.3.2

its not working brother

 

public function __construct(
\Magento\Catalog\Block\Product\Context $context,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
\Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
\Magento\Framework\App\Http\Context $httpContext,
\artisticbird\ShopByBrand\Model\BrandFactory $brandFactory,
        
array $data = []
) {
$this->_productCollectionFactory = $productCollectionFactory;
$this->_categoryCollectionFactory = $categoryCollectionFactory;
$this->_catalogProductVisibility = $catalogProductVisibility;
$this->httpContext = $httpContext;
$this->_imageHelper = $context->getImageHelper();
$this->_brandFactory = $brandFactory;
$this->_cartHelper = $context->getCartHelper();
parent::__construct(
$context,
$data
);
        $this->setCollection($this->getProductCollection());
       $this->setCollections($this->getCategoryCollection());
}