I want to filter category by store id. Used the following code, but it is not working.
$collection = $this->categoryCollectionFactory->create(); $collection->addAttributeToSelect('*') ->setStoreId(0);
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityI want to filter category by store id. Used the following code, but it is not working.
$collection = $this->categoryCollectionFactory->create(); $collection->addAttributeToSelect('*') ->setStoreId(0);
Try below code. this is working for me.
$collection = $this->categoryCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->addStoreFilter($this->_storeManager->getStore($store_id));
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$categoryCollection = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
$categories = $categoryCollection->create();
$categories->addAttributeToSelect('*');
$categories->setStore('0'); /* set here your store id */
foreach ($categories as $category) {
echo $category->getName() . '<br />';
}
$collection = $this->categoryCollectionFactory->create();
$collection->addAttributeToSelect('*')
->setStore($storeId);
instead ->setStoreId(0); use ->setStore(0);