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
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 !
its not working brother