How to get Category Collection per Store in Magento 2?
Use directly objectmanager is not best/Recommended way to do in magento use Block with consturct and fetch method in your phtml file.
$categoryFactory = $objectManager->create('Magento\Catalog\Helper\Category');
$categoryFactory->getStoreCategories(false,false,true);
For More details kindly refer blogs link, Category Collection per store wise
Using Block way,
class Categorydata extends \Magento\Framework\View\Element\Template {
protected $_categoryHelper;
protected $categoryFactory;
protected $_catalogLayer;
public function __construct(
\Magento\Catalog\Block\Product\Context $context,
\Magento\Catalog\Helper\Category $categoryHelper,
array $data = []
) {
$this->_categoryHelper = $categoryHelper;
parent::__construct(
$context,
$data
);
}
/**
* Retrieve current store level 2 category
*
* @param bool|string $sorted (if true display collection sorted as name otherwise sorted as based on id asc)
* @param bool $asCollection (if true display all category otherwise display second level category menu visible category for current store)
* @param bool $toLoad
*/
public function getStoreCategories($sorted = false, $asCollection = false, $toLoad = true)
{
return $this->_categoryHelper->getStoreCategories($sorted , $asCollection, $toLoad);
}
}
call inside phtml file,
$categorys = $this->getStoreCategories(false,false,true);
foreach($categorys as $category){
echo $category->getName()
}
Create Block and add below code to your block.
namespace <vendor>\<module>\Block;
class FeaturedCategories extends \Magento\Framework\View\Element\Template{
protected $_categoryCollection;
protected $_storeManager;
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollection,
array $data = []
)
{
$this->_categoryCollection = $categoryCollection;
$this->_storeManager = $storeManager;
parent::__construct($context, $data);
}
public function getCategoryCollection()
{
$collection = $this->_categoryCollection->create()
->addAttributeToSelect('*')
->setStore($this->_storeManager->getStore())
//->addAttributeToFilter('attribute_code', '1')
->addAttributeToFilter('is_active','1');
return $collection;
}
}
And $block->getCategoryCollection() used this in your template file. to get category collection
Try this :
protected $_storeManager;
public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager,
$data = []
) {
$this->_storeManager = $storeManager;
parent::__construct($data);
}
$objectManager = $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$categoryFactory = $objectManager->create('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
$categories = $categoryFactory->create()
->addAttributeToSelect('*')
->setStore($this->_storeManager->getStore()); //categories from current store will be fetched
foreach ($categories as $category){
$category->getName();
}